Skip to content

Instantly share code, notes, and snippets.

@JayH5
Last active April 5, 2018 10:31
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 JayH5/942ceb4a79c6e44fe15ba9af76f97868 to your computer and use it in GitHub Desktop.
Save JayH5/942ceb4a79c6e44fe15ba9af76f97868 to your computer and use it in GitHub Desktop.
Elixir example: Anagram counter
defmodule Anagrams do
def anagram_counter(words, n) do
words
|> Enum.filter(fn word -> String.length(word) == n end)
|> Enum.reduce(%{}, fn word, lookup ->
Map.update(lookup, lookup_key(word), MapSet.new([word]), &MapSet.put(&1, word))
end)
|> Enum.count(fn {_key, anagram_words} -> MapSet.size(anagram_words) > 1 end)
end
defp lookup_key(word),
do: word |> String.downcase() |> String.graphemes() |> Enum.sort() |> Enum.join()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment