Guest User

Untitled

a guest
Apr 10th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 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.     print "Text length:", len(text)
  16.     print "Start:", start
  17.     print "Text to shift:", text[start:], "\n"
  18.     print "Beginning loop:"
  19.    
  20.     def get_word_and_space(text):
  21.         first_word = ''
  22.         for char in text:
  23.             if char != ' ':
  24.                 first_word += char
  25.             else:
  26.                 first_word += ' '
  27.                 break
  28.         return first_word
  29.  
  30.     for x in range(0,28):
  31.         shifted_text = apply_shift(text[:], x)
  32.         #splitted = shifted_text[start:].partition(' ')
  33.         splitted = get_word_and_space(shifted_text[start:])
  34.         print "x is:", x
  35.         print "Potential splitted:", splitted
  36.         if is_word(wordlist, splitted) is True:
  37.             print "\n\n", splitted, "\n\n"
  38.             print start, x
  39.             print "lensplit plus start is", len(splitted) + start, "\n"
  40.             print "-------------------------------------------------------"
  41.             print shifted_text
  42.             print shifted_text[(len(splitted)+ start + 1):]
  43.             print "-------------------------------------------------------"
  44.             return [(start, x)] + find_best_shifts_rec(wordlist, shifted_text, len(splitted)+ start + 0)
  45.    
  46.     if start >= len(text) or text[start:] in string.punctuation:
  47.         return []
Advertisement
Add Comment
Please, Sign In to add comment