Skip to content

Instantly share code, notes, and snippets.

@jdunne
Created August 5, 2017 02:23
Show Gist options
  • Save jdunne/40a90cd3faffd12f480733c1d4162c36 to your computer and use it in GitHub Desktop.
Save jdunne/40a90cd3faffd12f480733c1d4162c36 to your computer and use it in GitHub Desktop.
Anagram Finder
"""
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)
@jdunne
Copy link
Author

jdunne commented Aug 5, 2017

This english-words repo is useful for testing this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment