Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find_best_shifts_rec(wordlist, text, start=0):
- shifts = []
- for shift in range(26):
- # creates a string and only shifts from the start point
- testtext = apply_shift(text[start:], -shift)
- testlist = testtext.split()
- # words made by the current shift
- realwords = []
- for word in testlist:
- if word in wordlist:
- realwords.append(word)
- else: # as soon as an invalid word is found I know the shift is invalid
- break
- if realwords: # if one or more words are real
- # add the location and magnitude of shift
- shifts = [(start, shift)]
- # recursive call - start needs to be the end of the last valid word
- realword = realwords[-1]
- start += testtext.rindex(realword) + len(realword) + 1
- if start >= len(text):
- return shifts # base case
- return shifts + find_best_shifts_rec(wordlist, text, start)
- return shifts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement