Created
August 5, 2017 02:23
-
-
Save jdunne/40a90cd3faffd12f480733c1d4162c36 to your computer and use it in GitHub Desktop.
Anagram Finder
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
""" | |
anagram.py - Finds anagrams in stdin. | |
""" | |
import sys | |
import collections | |
anagrams = collections.defaultdict(list) | |
for line in sys.stdin: | |
word = line.rstrip() | |
sorted_word = ''.join(sorted(list(word))) | |
anagrams[sorted_word].append(word) | |
for k, v in anagrams.iteritems(): | |
if len(v) > 1: | |
print ', '.join(v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This english-words repo is useful for testing this script.