Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find_best_shifts_rec(wordlist, text, start):
- """
- Given a scrambled string and a starting position from which
- to decode, returns a shift key that will decode the text to
- words in wordlist, or None if there is no such key.
- Hint: You will find this function much easier to implement
- if you use recursion.
- wordlist: list of words
- text: scambled text to try to find the words for
- start: where to start looking at shifts
- returns: list of tuples. each tuple is (position in text, amount of shift)
- """
- #raw_input(text)
- if len(text)<2:
- return []
- resultstr=text
- resultshift=28
- n=-1
- for shift in range(27):
- shifted=apply_shift(text,shift)
- firstword=shifted.split()[0]
- if is_word(wordlist,firstword):
- #print firstword
- n=len(firstword)+1
- resultshift,resultstr=shift,shifted
- break
- if resultshift>27:
- raw_input("%s,%s,%s" % (resultstr[n:],start,len(text)))
- return find_best_shifts_rec(wordlist,resultstr[n:],n+start)
- #raw_input(str([(start,resultshift),firstword,n]))
- return [(start,resultshift)]+find_best_shifts_rec(wordlist,resultstr[n:],n+start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement