Introduction to Computer Science I
Homework 6
Due by 1:30pm on Tuesday, February 22
Reading
Read Chapter 5 of the textbook.
Problems
Solve the following by implementing the corresponding functions in homework6.py. Also, document each function
by writing a short docstring explaining the purpose of the function.
When done, submit that file through D2L.
[Lab] Problems 5.27, 5.37, and 5.49.
Also do problem 5.48 as well as the following additional problems:
Additional Problem 1: The mirror image of string vow
is string wov, and the mirror image wood is
string boow. The mirror image of string bed
cannot be represented as a string, however, because the mirror image
of e is not a valid character. The characters in the
alphabet whose mirror image is a valid character are: b, d,
i, o, v, w, and x.
Develop function mirror() that takes a string and returns
its mirror image but only if the mirror image can be represented
using letters in the alphabet.
Usage:
>>> mirror('vow')
'wov'
>>> mirror('wood')
'boow'
>>> mirror('bed')
'INVALID'
Additional Problem 2: A very simplistic scheme, which was used
at one time to encode information, is to rotate the characters
within an alphabet and rewrite them. ROT13 is the variant in which
the characters A-Z are rotated 13 places, and it was a commonly used
insecure scheme that attempted to "hide" data in many applications
from the late 1990's and into the early 2000's.
It has been decided by Insecure Inc. to develop a product that
"improves" upon this scheme by first reversing the entire string and
then rotating it. As an example, if we apply this scheme to string ABCD
with a reversal and rotation of 1, after the reversal we would have
DCBA and then after rotating that by 1 position we have the
result EDCB. Your task is to implement this encoding
scheme for strings that contain only capital letters, underscores,
and periods. Rotations are to be performed using the alphabet order:
ABCDEFGHIJKLMNOPQRSTUVWXYZ_.
Note that underscore follows Z, and the period follows the
underscore. Thus a forward rotation of 1 means 'A' is shifted to
'B', that is, 'A'→'B', 'B'→'C', ..., 'Z'→'_', '_'→'.', and '.'→'A'.
Likewise a rotation of 3 means 'A'→'D', 'B'→'E', ..., '.'→'C'.
Write a function rrot() that takes as input the amount of rotation rot
and a string s that may contain only capital letters,
underscores, and periods and that returns a copy of the string s
after being reversed and then shifted by the specified amount rot.
Usage:
>>> rrot(1, 'ABCD')
'EDCB'
>>> rrot(3, 'YO_THERE.')
'CHUHKWBR.'
>>> rrot(1, '.DOT')
'UPEA'
>>> rrot(14, 'ROAD')
'ROAD'
>>> rrot(9,
'SHIFTING_AND_ROTATING_IS_NOT_ENCRYPTING')
'PWRAYF_LWNHAXWH.RHPWRAJAX_HMWJHPWRAORQ.'
>>> rrot(1, 'SNQZDRQDUDQ')
'REVERSE_ROT'