Advertisement
IHATEMICROWAVEOVEN

words combined at best spot

Nov 15th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. baseWord = input("Enter a word: ")
  2.  
  3.  
  4. while len(baseWord)!=1:
  5. word2 = input("Enter another word to add to the end of that word: ")
  6.  
  7. # First of all, find the best match
  8. bestCandidate = baseWord
  9. bestSum = 999 # the smallest sum of the two indexes
  10.  
  11. word2Index = 0
  12. while word2Index<len(word2): # while loop that runs through word2 left to right
  13. baseWordIndex = len(baseWord)-1
  14. while baseWordIndex>=0: # while loop that runs through baseWord right to left
  15.  
  16. if (baseWord[baseWordIndex]==word2[word2Index] and # if characters match, and
  17. (len(baseWord)-baseWordIndex)+word2Index<bestSum): # if this is a better mix than the current best
  18. # update the best candidate to be this mix
  19. bestCandidate = baseWord[0:baseWordIndex] + word2[word2Index:]
  20. bestSum = (len(baseWord)-baseWordIndex)+word2Index
  21.  
  22. baseWordIndex -= 1
  23. word2Index += 1
  24.  
  25. print("The result: "+bestCandidate)
  26. baseWord = bestCandidate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement