Created
June 18, 2011 21:41
-
-
Save jgeewax/1033532 to your computer and use it in GitHub Desktop.
Anagram data structure
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
from collections import defaultdict | |
words = ['eat', 'tea', 'ate', 'time'] # ... get words from somewhere else | |
data = defaultdict(set) | |
for word in words: | |
data[''.join(sorted(word))].add(word) | |
def get_anagrams_for(word): | |
words = data[''.join(sorted(word))] | |
words.remove(word) | |
return list(words) | |
if __name__ == '__main__': | |
print get_anagrams_for('tea') # prints ['ate', 'eat'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mines not as good - but still fun.