Skip to content

Instantly share code, notes, and snippets.

@josevalim
Created May 25, 2015 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josevalim/28664c78032b7f6b5b71 to your computer and use it in GitHub Desktop.
Save josevalim/28664c78032b7f6b5b71 to your computer and use it in GitHub Desktop.
defmodule Anagrams do
def main(args) do
path = hd(args)
file = File.stream!(path)
words = gather_words(file, 4)
print(words, 4)
end
def gather_words(file, min) do
Enum.reduce file, HashDict.new, fn line, word_map ->
word = String.strip line
key = Enum.sort to_char_list(word)
if length(key) >= min do
Dict.update(word_map, key, [word], fn current -> [word|current] end)
else
word_map
end
end
end
def print(words, min) do
Enum.each Dict.keys(words), fn key ->
if length(words[key]) >= min, do: IO.puts Enum.join words[key], " "
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment