2 from language_models
import *
4 def caesar_encipher_letter(accented_letter
, shift
):
5 """Encipher a letter, given a shift amount
7 >>> caesar_encipher_letter('a', 1)
9 >>> caesar_encipher_letter('a', 2)
11 >>> caesar_encipher_letter('b', 2)
13 >>> caesar_encipher_letter('x', 2)
15 >>> caesar_encipher_letter('y', 2)
17 >>> caesar_encipher_letter('z', 2)
19 >>> caesar_encipher_letter('z', -1)
21 >>> caesar_encipher_letter('a', -1)
23 >>> caesar_encipher_letter('A', 1)
25 >>> caesar_encipher_letter('é', 1)
28 letter
= unaccent(accented_letter
)
29 if letter
in string
.ascii_letters
:
30 if letter
in string
.ascii_uppercase
:
31 alphabet_start
= ord('A')
33 alphabet_start
= ord('a')
34 return chr(((ord(letter
) - alphabet_start
+ shift
) % 26) +
39 def caesar_decipher_letter(letter
, shift
):
40 """Decipher a letter, given a shift amount
42 >>> caesar_decipher_letter('b', 1)
44 >>> caesar_decipher_letter('b', 2)
47 return caesar_encipher_letter(letter
, -shift
)
49 def caesar_encipher(message
, shift
):
50 """Encipher a message with the Caesar cipher of given shift
52 >>> caesar_encipher('abc', 1)
54 >>> caesar_encipher('abc', 2)
56 >>> caesar_encipher('abcxyz', 2)
58 >>> caesar_encipher('ab cx yz', 2)
60 >>> caesar_encipher('Héllo World!', 2)
63 enciphered
= [caesar_encipher_letter(l
, shift
) for l
in message
]
64 return ''.join(enciphered
)
66 def caesar_decipher(message
, shift
):
67 """Decipher a message with the Caesar cipher of given shift
69 >>> caesar_decipher('bcd', 1)
71 >>> caesar_decipher('cde', 2)
73 >>> caesar_decipher('cd ez ab', 2)
75 >>> caesar_decipher('Jgnnq Yqtnf!', 2)
78 return caesar_encipher(message
, -shift
)
81 if __name__
== "__main__":