Here is my Countdown word solver.
This code aims to find all possible permutations of any length of a given input (which in the Countdown words game is any random assortment of letters). It then compares each permutation with the dictionary and prints it if it is a valid word. For example the letters "PHAYPC" could yield the following words (not limited to):
- Chap
- Happy
- Hay
I've used the enchant library as my dictionary checker.
import itertools
import enchant
word = input("Please enter the letters or words to scramble: ")
perm_words = itertools.chain.from_iterable(itertools.permutations(word, i) for i in range(3,len(word)))
list_possible = []
list_words_print = []
for item in perm_words:
list_possible.append("".join(item).lower())
dictionary = enchant.Dict("en_UK")
for word in list_possible:
if dictionary.check(word):
if word not in list_words_print: list_words_print.append(word)
print (list(item for item in list_words_print))
exit_key = input("Press any key to exit")