I'm doing a simple python project for school which involves the user trying to find the longest real word they can from a random pool of 9 letters in 30 seconds. It's easy enough to test if the user's answer is a real word, by looping through a file of many words in the English language (it has 114,000 words which probably doesn't include ALL of them). In fact, that took a split-second to run.
However, in order to check if there is a full 'solution' (9-letter real word) for the random pool of letters, I couldn't think of anything apart from testing for every single permutation of the 9-letter word pool against every word in the file. The problem is, that's really inefficient and took about 7 minutes. After all, considering that there are 9! (factorial) permutations of the anagram, and that the 'random' module does NOT rule out previous permutations, AND that there are many words in the file, the computer has to work through over 50 billion comparisons. At the end of it, the word (using an anagram of 'happiness') was not found. This is the code I tried:
# python 3.5.2
import random
anagram = ['p','s','h','a','i','s','n','p','e'] # anagram of hapiness
with open('wordlist.txt') as in_file:
for line in in_file:
line = line.rstrip()
shuffledList = random.sample(anagram, len(anagram)) # randomise order
shuffledWord = ''.join(shuffledList) # make it a string
if shuffledWord == line:
print("YES") # (never happens)
break
Apart from code that actually yields a solution, I'm looking for some sort of clever algorithmic solution/ threading technique/ absolutely anything, that could find the original word of a 9-letter anagram in 30 seconds or less. It seems really far-fetched but I thought it was worth a try. Any suggestions?