2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -2,24 +2,15 @@ defmodule Anagrams do
|
|||
def find(file) do
|
||||
File.read!(file)
|
||||
|> String.split
|
||||
|> Enum.map(&String.codepoints &1)
|
||||
|> sort(%{})
|
||||
|> Enum.group_by(fn word -> String.codepoints(word) |> Enum.sort end)
|
||||
|> Enum.group_by(fn {_,v} -> length(v) end)
|
||||
|> Enum.max
|
||||
|> print
|
||||
end
|
||||
|
||||
defp sort([],m), do: m
|
||||
defp sort([word|words],m) do
|
||||
s = Enum.sort(word)
|
||||
m = Dict.update(m, s, [word], fn val -> [word|val] end)
|
||||
sort(words,m)
|
||||
end
|
||||
|
||||
defp print({_,y}) do
|
||||
Enum.each(y, fn {_,e} ->
|
||||
Enum.map(e, &Enum.join(&1)) |> Enum.sort |> Enum.join(" ") |> IO.puts
|
||||
end)
|
||||
Enum.each(y, fn {_,e} -> Enum.sort(e) |> Enum.join(" ") |> IO.puts end)
|
||||
end
|
||||
end
|
||||
|
||||
Anagrams.find("unixdict.txt")
|
||||
|
|
|
|||
|
|
@ -1,13 +1,8 @@
|
|||
File.stream!("unixdict.txt")
|
||||
|> Stream.map(&String.strip &1)
|
||||
|> Stream.map(&{&1, &1 |> String.codepoints |> Enum.sort |> Enum.join})
|
||||
|> Enum.group_by(fn {_,y} -> y end)
|
||||
|> Dict.values
|
||||
|> Enum.group_by(&length(&1))
|
||||
|> Enum.group_by(&String.codepoints(&1) |> Enum.sort)
|
||||
|> Map.values
|
||||
|> Enum.group_by(&length &1)
|
||||
|> Enum.max
|
||||
|> elem(1)
|
||||
|> Enum.each(fn n -> Enum.map(n, fn {y,_} -> y end)
|
||||
|> Enum.sort
|
||||
|> Enum.join(" ")
|
||||
|> IO.puts
|
||||
end)
|
||||
|> Enum.each(fn n -> Enum.sort(n) |> Enum.join(" ") |> IO.puts end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue