Skip to content

Instantly share code, notes, and snippets.

@lgalke
Last active March 12, 2020 15:09
Show Gist options
  • Save lgalke/8a30a224de9d2b19da1674ba46548ce6 to your computer and use it in GitHub Desktop.
Save lgalke/8a30a224de9d2b19da1674ba46548ce6 to your computer and use it in GitHub Desktop.
Simple Anagram Generator in Python
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