Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- baseWord = input("Enter a word: ")
- while len(baseWord)!=1:
- word2 = input("Enter another word to add to the end of that word: ")
- # First of all, find the best match
- bestCandidate = baseWord
- bestSum = 999 # the smallest sum of the two indexes
- word2Index = 0
- while word2Index<len(word2): # while loop that runs through word2 left to right
- baseWordIndex = len(baseWord)-1
- while baseWordIndex>=0: # while loop that runs through baseWord right to left
- if (baseWord[baseWordIndex]==word2[word2Index] and # if characters match, and
- (len(baseWord)-baseWordIndex)+word2Index<bestSum): # if this is a better mix than the current best
- # update the best candidate to be this mix
- bestCandidate = baseWord[0:baseWordIndex] + word2[word2Index:]
- bestSum = (len(baseWord)-baseWordIndex)+word2Index
- baseWordIndex -= 1
- word2Index += 1
- print("The result: "+bestCandidate)
- baseWord = bestCandidate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement