Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. def find_best_shifts_rec(wordlist, text, start=0):
  2.  
  3. shifts = []
  4.  
  5. for shift in range(26):
  6. # creates a string and only shifts from the start point
  7. testtext = apply_shift(text[start:], -shift)
  8. testlist = testtext.split()
  9. # words made by the current shift
  10. realwords = []
  11. for word in testlist:
  12.  
  13. if word in wordlist:
  14. realwords.append(word)
  15. else: # as soon as an invalid word is found I know the shift is invalid
  16. break
  17.  
  18. if realwords: # if one or more words are real
  19. # add the location and magnitude of shift
  20. shifts = [(start, shift)]
  21. # recursive call - start needs to be the end of the last valid word
  22. realword = realwords[-1]
  23. start += testtext.rindex(realword) + len(realword) + 1
  24. if start >= len(text):
  25. return shifts # base case
  26. return shifts + find_best_shifts_rec(wordlist, text, start)
  27.  
  28. return shifts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement