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)
- """
- print "Text length:", len(text)
- print "Start:", start
- print "Text to shift:", text[start:], "\n"
- print "Beginning loop:"
- def get_word_and_space(text):
- first_word = ''
- for char in text:
- if char != ' ':
- first_word += char
- else:
- first_word += ' '
- break
- return first_word
- for x in range(0,28):
- shifted_text = apply_shift(text[:], x)
- #splitted = shifted_text[start:].partition(' ')
- splitted = get_word_and_space(shifted_text[start:])
- print "x is:", x
- print "Potential splitted:", splitted
- if is_word(wordlist, splitted) is True:
- print "\n\n", splitted, "\n\n"
- print start, x
- print "lensplit plus start is", len(splitted) + start, "\n"
- print "-------------------------------------------------------"
- print shifted_text
- print shifted_text[(len(splitted)+ start + 1):]
- print "-------------------------------------------------------"
- return [(start, x)] + find_best_shifts_rec(wordlist, shifted_text, len(splitted)+ start + 0)
- if start >= len(text) or text[start:] in string.punctuation:
- return []
Advertisement
Add Comment
Please, Sign In to add comment