Skip to content

Instantly share code, notes, and snippets.

@techbelly
Created January 8, 2011 00:32
Show Gist options
  • Save techbelly/770367 to your computer and use it in GitHub Desktop.
Save techbelly/770367 to your computer and use it in GitHub Desktop.
words = (w.strip().lower() for w in open('/usr/share/dict/words'))
#words = "now is the time for all good lal men godo".split()
def charsorted(word):
return "".join(sorted(list(word)))
results = dict()
for word,index in ((word,charsorted(word)) for word in words):
if index not in results:
results[index] = []
results[index].append(word)
anagrams = (anagrams for anagrams in results.itervalues() if len(anagrams)>1)
for i,anagram in enumerate(anagrams,1):
print i," ".join(anagram)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment