Skip to content

Instantly share code, notes, and snippets.

@tripy
Created September 17, 2015 22:54
Show Gist options
  • Save tripy/1487e657985542a09b87 to your computer and use it in GitHub Desktop.
Save tripy/1487e657985542a09b87 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
""Finds all anagrams from a dictionary files for the letters provided
as argument"""
import sys
def is_anagram(letters, word):
letters_a = list(letters)
try:
[letters_a.remove(x) for x in word]
except ValueError:
return False
return True
if __name__ == "__main__":
try:
letters = sys.argv[1]
except IndexError:
print "<command> <list of letters>"
sys.exit(1)
letters_len = len(letters)
good_words = set()
with open('american-english-large', 'r') as dict_words:
for line in dict_words:
word = line[:-1]
if len(word) == 1:
continue
if len(word) <= letters_len:
if is_anagram(letters, word):
good_words.add(word)
for r_word in sorted(good_words, key=len):
print r_word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment