Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Levenshtein-distance/Elixir/levenshtein-distance.elixir
Normal file
28
Task/Levenshtein-distance/Elixir/levenshtein-distance.elixir
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
defmodule Levenshtein do
|
||||
def distance(a, b) do
|
||||
ta = String.downcase(a) |> to_charlist |> List.to_tuple
|
||||
tb = String.downcase(b) |> to_charlist |> List.to_tuple
|
||||
m = tuple_size(ta)
|
||||
n = tuple_size(tb)
|
||||
costs = Enum.reduce(0..m, %{}, fn i,acc -> Map.put(acc, {i,0}, i) end)
|
||||
costs = Enum.reduce(0..n, costs, fn j,acc -> Map.put(acc, {0,j}, j) end)
|
||||
Enum.reduce(0..n-1, costs, fn j, acc ->
|
||||
Enum.reduce(0..m-1, acc, fn i, map ->
|
||||
d = if elem(ta, i) == elem(tb, j) do
|
||||
map[ {i,j} ]
|
||||
else
|
||||
Enum.min([ map[ {i , j+1} ] + 1, # deletion
|
||||
map[ {i+1, j } ] + 1, # insertion
|
||||
map[ {i , j } ] + 1 ]) # substitution
|
||||
end
|
||||
Map.put(map, {i+1, j+1}, d)
|
||||
end)
|
||||
end)
|
||||
|> Map.get({m,n})
|
||||
end
|
||||
end
|
||||
|
||||
words = ~w(kitten sitting saturday sunday rosettacode raisethysword)
|
||||
Enum.each(Enum.chunk(words, 2), fn [a,b] ->
|
||||
IO.puts "distance(#{a}, #{b}) = #{Levenshtein.distance(a,b)}"
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue