Advertisement
Guest User

Untitled

a guest
Nov 25th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. # Problem 4: Multi-level decryption.
  2. #
  3.  
  4.  
  5. def find_best_shifts(wordlist, text):
  6.     """
  7.    Given a scrambled string, returns a shift key that will decode the text to
  8.    words in wordlist, or None if there is no such key.
  9.  
  10.    Hint: Make use of the recursive function
  11.    find_best_shifts_rec(wordlist, text, start)
  12.  
  13.    wordlist: list of words
  14.    text: scambled text to try to find the words for
  15.    returns: list of tuples.  each tuple is (position in text, amount of shift)
  16.    
  17.    Examples:
  18.    >>> s = random_scrambled(wordlist, 3)
  19.    >>> s
  20.    'eqorqukvqtbmultiform wyy ion'
  21.    >>> shifts = find_best_shifts(wordlist, s)
  22.    >>> shifts
  23.    [(0, 25), (11, 2), (21, 5)]
  24.    >>> apply_shifts(s, shifts)
  25.    'compositor multiform accents'
  26.    >>> s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)])
  27.    >>> s
  28.    'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?'
  29.    >>> shifts = find_best_shifts(wordlist, s)
  30.    >>> print apply_shifts(s, shifts)
  31.    Do Androids Dream of Electric Sheep?
  32.    """
  33.  
  34. def find_best_shifts_rec(wordlist, text, start):
  35.     """
  36.    Given a scrambled string and a starting position from which
  37.    to decode, returns a shift key that will decode the text to
  38.    words in wordlist, or None if there is no such key.
  39.  
  40.    Hint: You will find this function much easier to implement
  41.    if you use recursion.
  42.  
  43.    wordlist: list of words
  44.    text: scambled text to try to find the words for
  45.    start: where to start looking at shifts
  46.    returns: list of tuples.  each tuple is (position in text, amount of shift)
  47.    """
  48.     ### TODO.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement