Anonymous user
Longest common prefix: Difference between revisions
Replace Ruby version with Elixir
Not a robot (talk | contribs) (Add CLU) |
(Replace Ruby version with Elixir) |
||
Line 1,371:
=={{header|Elixir}}==
<lang elixir>
@data [
def lcp([]), do: ""▼
["interspecies", "interstellar", "interstate"],▼
def lcp(strs) do▼
["throne", "throne"],▼
["throne", "dungeon"],▼
["throne", "", "throne"],▼
["cheese"],▼
[""],▼
[],▼
["prefix", "suffix"],▼
["foo", "foobar"]▼
]
def main do
Enum.each(@data, fn strs ->▼
end)
end
end▼
defp lcpstr([]), do: ""
▲ ["interspecies","interstellar","interstate"],
strs |> Enum.map(&String.to_charlist/1) |> lcp() |> IO.chardata_to_string()
▲ ["throne","throne"],
end
▲ ["throne","dungeon"],
▲ ["throne","","throne"],
▲ ["cheese"],
defp lcp(strs), do: Enum.reduce(strs, &lcp/2)
▲ [""],
▲ [],
defp lcp(xs, ys), do: lcp(xs, ys, [])
▲ ["prefix","suffix"],
▲ ["foo","foobar"]
defp lcp([x | xs], [x | ys], pre), do: lcp(xs, ys, [x | pre])
defp lcp(_, _, pre), do: Enum.reverse(pre)
▲end
▲Enum.each(data, fn strs ->
▲ IO.puts "lcp(#{inspect strs}) = #{inspect RC.lcp(strs)}"
▲end)</lang>
{{out}}
<pre>
</pre>
|