1.Write a function encipher(s, n) that takes as inputs an arbitrary string s and a non-negative integer n between 0 and 25, and that returns a new string in which the letters in s have been "rotated" by n characters forward in the alphabet, wrapping around as needed. For example:

>>> encipher('hello', 1) result: 'ifmmp'

>>> encipher('hello', 2) result: 'jgnnq'

>>> encipher('hello', 4) result: 'lipps'

Upper-case letters should be "rotated" to upper-case letters, even if you need to wrap around. For example:

>>> encipher('XYZ', 3)

result: 'ABC'

Lower-case letters should be "rotated" to lower-case letters:

>>> encipher('xyz', 3)

result: 'abc'

Non-alphabetic characters should be left unchanged:

>>> encipher('#caesar!', 2)

result: '#ecguct!'

Ā

**Hints/reminders:**

You can use the built-in functions ord and chr convert from single-character strings to integers and back:

>>> ord('a')

result: 97

>>> chr(97)

result: 'a'

You can use the following test to determine if a character is between 'a' and 'z' in the alphabet:

if 'a' <= c <= 'z':

A similar test will work for upper-case letters.

We recommend writing a helper function rot(c, n) that rotates a single character c forward by n spots in the alphabet. We have given you a template for this helper function in ps3pr3.py that checks to ensure that c is a single-character string. We wrote rot13(c) in lecture; rot(c, n) will beĀ veryĀ close to rot13(c)! You can test your rot(c, n) as follows:

>>> rot('a', 1)

result: 'b'

>>> rot('y', 2)

result: 'a'

>>> rot('A', 3)

result: 'D'

>>> rot('Y', 3)

result: 'B'

>>> rot('!', 4)

result: '!'

Once you have rot(c, n), you can write a recursive encipher function.

Once you think you have everything working, here are three more examples to try:

>>> encipher('xyza', 1)

result: 'yzab'

>>> encipher('Z A', 2)

result: 'B C'

>>> encipher('Caesar cipher? I prefer Caesar salad.', 25)

result: 'Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.'

Ā

2.Write a function decipher(s) that takes as input an arbitrary string s that has already been enciphered by having its characters "rotated" by some amount (possibly 0). decipher should return, to the best of its ability, theĀ originalĀ English string, which will be some rotation (possibly 0) of the input string s. For example:

>>> decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.')

result: 'Caesar cipher? I prefer Caesar salad.'

Here are two more examples:

>>> decipher('Hu lkbjhapvu pz doha ylthpuz hmaly dl mvynla lclyfaopun dl ohcl slhyulk.')

result: 'An education is what remains after we forget everything we have learned.'

>>> decipher('python')

result: 'eniwdc'

------------------------------------

def rot(c, n):

Ā Ā """ your docstring goes here """

Ā Ā # check to ensure that c is a single character

Ā Ā assert(type(c) == str and len(c) == 1)

def letter_prob(c):

Ā Ā """ if c is the space character (' ') or an alphabetic character,

Ā Ā Ā Ā returns c's monogram probability (for English);

Ā Ā Ā Ā returns 1.0 for any other character.

Ā Ā Ā Ā adapted from:

Ā Ā Ā Ā

Ā Ā """

Ā Ā # check to ensure that c is a single character Ā Ā

Ā Ā assert(type(c) == str and len(c) == 1)

Ā Ā if c == ' ': return 0.1904

Ā Ā if c == 'e' or c == 'E': return 0.1017

Ā Ā if c == 't' or c == 'T': return 0.0737

Ā Ā if c == 'a' or c == 'A': return 0.0661

Ā Ā if c == 'o' or c == 'O': return 0.0610

Ā Ā if c == 'i' or c == 'I': return 0.0562

Ā Ā if c == 'n' or c == 'N': return 0.0557

Ā Ā if c == 'h' or c == 'H': return 0.0542

Ā Ā if c == 's' or c == 'S': return 0.0508

Ā Ā if c == 'r' or c == 'R': return 0.0458

Ā Ā if c == 'd' or c == 'D': return 0.0369

Ā Ā if c == 'l' or c == 'L': return 0.0325

Ā Ā if c == 'u' or c == 'U': return 0.0228

Ā Ā if c == 'm' or c == 'M': return 0.0205

Ā Ā if c == 'c' or c == 'C': return 0.0192

Ā Ā if c == 'w' or c == 'W': return 0.0190

Ā Ā if c == 'f' or c == 'F': return 0.0175

Ā Ā if c == 'y' or c == 'Y': return 0.0165

Ā Ā if c == 'g' or c == 'G': return 0.0161

Ā Ā if c == 'p' or c == 'P': return 0.0131

Ā Ā if c == 'b' or c == 'B': return 0.0115

Ā Ā if c == 'v' or c == 'V': return 0.0088

Ā Ā if c == 'k' or c == 'K': return 0.0066

Ā Ā if c == 'x' or c == 'X': return 0.0014

Ā Ā if c == 'j' or c == 'J': return 0.0008

Ā Ā if c == 'q' or c == 'Q': return 0.0008

Ā Ā if c == 'z' or c == 'Z': return 0.0005

Ā Ā return 1.0