Last active
March 12, 2020 15:09
-
-
Save lgalke/8a30a224de9d2b19da1674ba46548ce6 to your computer and use it in GitHub Desktop.
Simple Anagram Generator in Python
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
import argparse, itertools | |
parser = argparse.ArgumentParser() | |
parser.add_argument('input', help="Initial text for generating anagrams") | |
parser.add_argument('-d', '--dict', default='/usr/share/dict/cracklib-small', | |
help="A word list such as '/usr/share/dict/words' or '/usr/dict/words'") | |
args = parser.parse_args() | |
with open(args.dict, 'r') as fh: | |
valid_words = set(line.strip().lower() for line in fh) | |
anagrams = set(''.join(a) for a in itertools.permutations(list(args.input.lower())) if all(w in valid_words for w in ''.join(a).split())) | |
for anagram in sorted(anagrams): | |
print(anagram) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment