I used this hideous and inefficient implementation to find the word that can have the most consecutive last letters removed and still be a word.
Rodeo, for example, is a well-known one: Rodeo, Rode, Rod, Ro.
The program found ‘composers’: Composers, Composer, Compose, Compos, Comp
I was wondering how I should go about creating a program that finds the longest word that can have ANY of its letters (not just the last ones) removed and it still be considered a word:
For example: beast, best, bet, be — would be a valid possibility
Here was my program to find the one that removes consecutive letters (I’m also interested in hearing how this can be improved and optimized):
#Recursive function that finds how many letters can be removed from a word and
#it still be valid.
def wordCheck(word, wordList, counter):
if len(word)>=1:
if word in wordList:
return (wordCheck(word[0:counter-1], wordList, counter-1))
else:
return counter
return counter
def main():
a = open('C:\\Python32\\megalist2.txt', 'r+')
wordList = set([line.strip() for line in a])
#megaList contains a sorted list of tuple of
#(the word, how many letters can be removed consecutively)
megaList = sorted([(i, len(i)-1- wordCheck(i, wordList, len(i))) for i in wordList], key= lambda megaList: megaList[1])
for i in megaList:
if i[1] > 3:
print (i)
if __name__ == '__main__':
main()
Here’s an implementation I just wrote up. It runs in about five seconds with my ~235k word list. The output doesn’t show the whole chain, but you can easily reassemble it from the output.
The longest word chain in my dictionary is: