I am looking to build a list of words (three letters or more) that make up a large list of six-letter words. I was wondering if there was a cleaner or more efficient way to loop through the characters to build these subsets.
The top of this program (not posted) takes two .txt
files - one containing the list of 6-letter words and one containing the Scrabble Dictionary (used to determine what is a real word).
The main core of my code is below. I loop through each character of the word, making sure indexes don't repeat and then build each word subset. I then sort the list on length and convert it into a string via .join
to feed into another app I am writing for "subset guessing".
# Define Dictionary Lookup Function
def isDictionaryWord(word):
if (word.lower().strip()) in listDictionary:
return True
else:
return False
def createSubsets(word):
print 'Starting Word Subset - ' + word
starttime = datetime.datetime.now()
tempOutput = []
## 1-Letter Word Loop
for c in range(0, len(word)):
## 2-Letter Word Loop (skip)
for d in range(0, len(word)):
if (c != d):
## 3-Letter Word Loop
for e in range(0, len(word)):
if (c != e) and (d != e):
threeLetterWord = word[c] + word[d] + word[e]
if isDictionaryWord(threeLetterWord) and (threeLetterWord not in tempOutput):
tempOutput.append(threeLetterWord)
#print threeLetterWord
## 4-Letter Word Loop
for f in range(0, len(word)):
if (c != f) and (d != f) and (e != f):
fourLetterWord = word[c] + word[d] + word[e] + word[f]
if isDictionaryWord(fourLetterWord) and (fourLetterWord not in tempOutput):
tempOutput.append(fourLetterWord)
#print fourLetterWord
## 5-Letter Word Loop
for g in range(0, len(word)):
if (c != g) and (d != g) and (e != g) and (f != g):
fiveLetterWord = word[c] + word[d] + word[e] + word[f] + word[g]
if isDictionaryWord(fiveLetterWord) and (fiveLetterWord not in tempOutput):
tempOutput.append(fiveLetterWord)
#print fiveLetterWord
endtime = datetime.datetime.now()
## Sort Array by Length for Game Purposes & Convert to String
tempOutput.sort(key = len)
tempOutputStr = word + ": " + ", ".join(tempOutput)
print 'Total Time Taken (s) - ' + str((endtime - starttime).total_seconds())
print 'Total Subset Words - ' + str(len(tempOutput) - 1) + '\n'
print 'Temp Output Array - ' + str(tempOutput) + '\n'
print 'Temp Output String - ' + tempOutputStr
print '========================================'
# Print tempOutputStr
fOut.write(tempOutputStr + '\n')
# Loop through all 6-letter words and create subsets
for word in listSix:
createSubsets(word)