I want to take a set of 9 characters which is stored in an array and get all the combinations of strings that can be made with those characters. The result is then compared to a text file and if a word matches then that word should be output back to the user.
At the moment I'm using the itertools.combinations
function to try and get all the combinations - the problem I am having is that the program starts with all the 9 letter combinations and if none of these combinations matches a word from the text file then the program tries 8 letter combinations and so forth until a word is found or there are no words that match.
lengthofword = 9
foundwords = False
count = 0
while foundwords == False:
wordcombos = [''.join(i) for i in itertools.combinations(letters,lengthofword)]
#print(wordcombos)
#print(len(wordcombos))
count = 0
for i in range(0,len(wordcombos)):
if count == len(wordcombos)-1:
print("gone through each word")
if lengthofword > 0:
lengthofword = lengthofword -1
elif lengthofword == 0:
print("there are no words with those letters")
foundwords = True
elif wordcombos[i] in gamewords:
print("the best anwser is " +wordcombos[i])
foundwords = True
elif wordcombos[i] not in gamewords:
count = count + 1
print(count)
What should happen is that the 9 length combinations of the characters provided are compared to the text file - the problem is that itertools
is not showing all the combinations as some letters that are passed in are not used when the lengthofword
is decreased to see if there are any smaller words that match. For example if the letters RQLCWUOUI are passed in the word "curl" should be present under the 4 letter word combinations but is not there.