Skip to content

Instantly share code, notes, and snippets.

@gcampfield
Created August 16, 2015 19:12
Show Gist options
  • Save gcampfield/616ea06f9beb9fc7d573 to your computer and use it in GitHub Desktop.
Save gcampfield/616ea06f9beb9fc7d573 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import sys
import enchant
try:
input = raw_input
except NameError:
pass
def permutations(word):
if len(word) == 2:
return list(set([word, word[::-1]]))
res = []
for letter in set(word):
endings = permutations(word.replace(letter, '', 1))
res = res + [letter + ending for ending in endings] + endings
return list(set(res))
if len(sys.argv) == 1:
args = input('Enter letters: ')
params = []
else:
args = sys.argv[1]
if len(sys.argv) > 2:
params = [arg.split('-') for arg in sys.argv[2:]]
params = [(int(arg[0]), arg[1]) for arg in params]
d = enchant.Dict('en_US')
valid = []
for combination in permutations(args):
if d.check(combination):
for param in params:
if len(combination) < param[0] or combination[param[0]-1] != param[1]:
break
else:
valid.append(combination)
valid = sorted(valid, key=len, reverse=True)
for word in valid:
print(word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment