Problem 3. Multi-level Encryption & Decryption
Clearly the basic Caesar cipher is not terribly secure. To make things a little harder to crack, you
will now implement a multi-level Caesar cipher. Instead of shifting the entire string by a single
value, you will perform additional shifts at specified locations throughout the string. This
function takes a string text and a list of tuples shifts. The tuples in shifts represent the location of
the shift, and the shift itself. For example a tuple of (0,2) means that the shift starts are position 0
in the string and is a Caesar shift of 2. Additionally, the shifts are layered. This means that a set
of shifts [(0,2), (5, 3)] will first apply a Caesar shift of 2 to the entire string, and then apply a
Caesar shift of 3 starting at the 6th letter in the string.
To do this, implement the following function according to the specification.
def apply_shifts(text, shifts):
"""
Applies a sequence of shifts to an input text.
text: A string to apply the Ceasar shifts to
shifts: A list of tuples containing the location each shift should
begin and the shift offset. Each tuple is of the form (location,
shift) The shifts are layered: each one is applied from its
starting position all the way through the end of the string.
returns: text after applying the shifts to the appropriate
positions
Example:
>>> apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18),
(12, 16)])
'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?'
"""
### TODO.
Dostları ilə paylaş: |