Last active
April 5, 2018 10:31
-
-
Save JayH5/942ceb4a79c6e44fe15ba9af76f97868 to your computer and use it in GitHub Desktop.
Elixir example: Anagram counter
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 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