Last active
March 18, 2021 21:09
A command-line anagram solver using the Linux/OS X wordlist.
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
def anagram_solve(letters, words): | |
solution = [word.lower() for word in words if len(word) == len(letters)] | |
for letter in letters: | |
solution = [word for word in solution if letter in word and letters.count(letter) == word.count(letter)] | |
return solution | |
if __name__ == "__main__": | |
import sys | |
if len(sys.argv) == 1: | |
print("Usage: %s [anagram]" % sys.argv[0]) | |
else: | |
words = [word.rstrip() for word in open('/usr/share/dict/words', 'r')] | |
for anagram in sys.argv[1:]: | |
print("%s: %s" % (anagram, " ".join(anagram_solve(anagram, words)))) |
It's 2020, time to upgrade to Python 3.x
Honestly, I forgot about this code after 12 years! Thanks for the reminder. Easily updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Avinash,
It's 2020, time to upgrade to Python 3.x
I found this while playing with anagrams.
Rafael