-
-
Save josevalim/28664c78032b7f6b5b71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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