Skip to content

Instantly share code, notes, and snippets.

@almost
Created December 1, 2012 21:22
Show Gist options
  • Save almost/4185188 to your computer and use it in GitHub Desktop.
Save almost/4185188 to your computer and use it in GitHub Desktop.
# Another from Programming Pearls (again, my attempt before reading
# the rest of the chapter). Given a dictionary and a word, find all
# anagrams of the word in the dictionary.
words = (x.strip().lower() for x in open("/usr/share/dict/words"))
lookup = {}
for word in words:
letters = "".join(sorted(list(word)))
lookup.setdefault(letters, []).append(word)
def anagrams(word):
letters = "".join(sorted(list(word)))
return lookup.get(letters, [])
print anagrams("pots")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment