Advertisement
Guest User

Untitled

a guest
May 25th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. def find_best_shifts_rec(wordlist, text, start):
  2.     """
  3.    Given a scrambled string and a starting position from which
  4.    to decode, returns a shift key that will decode the text to
  5.    words in wordlist, or None if there is no such key.
  6.  
  7.    Hint: You will find this function much easier to implement
  8.    if you use recursion.
  9.  
  10.    wordlist: list of words
  11.    text: scambled text to try to find the words for
  12.    start: where to start looking at shifts
  13.    returns: list of tuples.  each tuple is (position in text, amount of shift)
  14.    """
  15.     #raw_input(text)
  16.     if len(text)<2:
  17.         return []
  18.     resultstr=text
  19.     resultshift=28
  20.     n=-1
  21.     for shift in range(27):
  22.         shifted=apply_shift(text,shift)
  23.         firstword=shifted.split()[0]
  24.         if is_word(wordlist,firstword):
  25.             #print firstword
  26.             n=len(firstword)+1
  27.             resultshift,resultstr=shift,shifted
  28.             break
  29.     if resultshift>27:
  30.         raw_input("%s,%s,%s" % (resultstr[n:],start,len(text)))
  31.         return find_best_shifts_rec(wordlist,resultstr[n:],n+start)
  32.     #raw_input(str([(start,resultshift),firstword,n]))
  33.     return [(start,resultshift)]+find_best_shifts_rec(wordlist,resultstr[n:],n+start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement