Skip to content

Instantly share code, notes, and snippets.

/anagrams.py Secret

Created April 11, 2015 10:11
Show Gist options
  • Save anonymous/49a8967c17c8361008cf to your computer and use it in GitHub Desktop.
Save anonymous/49a8967c17c8361008cf to your computer and use it in GitHub Desktop.
#encoding=utf-8
from collections import OrderedDict
def anagram_finder(word_list):
hash_dict = OrderedDict()
hash_word = lambda w: "".join(sorted(list(w)))
for word in word_list:
c = hash_word(word)
if c not in hash_dict:
hash_dict[c] = []
hash_dict[c].append(word)
return [val for key in hash_dict if len(hash_dict[key]) > 1 for val in hash_dict[key]]
if __name__ == "__main__":
words = "cool loco pretty stanley yelnats nice joke loop" \
" pool ploo egg geg mice men noodles poodles tasty testy" \
"mole lame satin stain"
print anagram_finder(words.split())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment