Created
December 1, 2012 21:22
-
-
Save almost/4185188 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
# 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