import string
+from language_models import *
-def caesar_encipher_letter(letter, shift):
+def caesar_encipher_letter(accented_letter, shift):
"""Encipher a letter, given a shift amount
>>> caesar_encipher_letter('a', 1)
'y'
>>> caesar_encipher_letter('a', -1)
'z'
+ >>> caesar_encipher_letter('A', 1)
+ 'B'
+ >>> caesar_encipher_letter('é', 1)
+ 'f'
"""
+ letter = unaccent(accented_letter)
if letter in string.ascii_letters:
if letter in string.ascii_uppercase:
alphabet_start = ord('A')
'cdezab'
>>> caesar_encipher('ab cx yz', 2)
'cd ez ab'
+ >>> caesar_encipher('Héllo World!', 2)
+ 'Jgnnq Yqtnf!'
"""
enciphered = [caesar_encipher_letter(l, shift) for l in message]
return ''.join(enciphered)
'abc'
>>> caesar_decipher('cd ez ab', 2)
'ab cx yz'
+ >>> caesar_decipher('Jgnnq Yqtnf!', 2)
+ 'Hello World!'
"""
return caesar_encipher(message, -shift)
--- /dev/null
+import unicodedata
+
+def unaccent(text):
+ """Remove all accents from letters.
+ It does this by converting the unicode string to decomposed compatability
+ form, dropping all the combining accents, then re-encoding the bytes.
+
+ >>> unaccent('hello')
+ 'hello'
+ >>> unaccent('HELLO')
+ 'HELLO'
+ >>> unaccent('héllo')
+ 'hello'
+ >>> unaccent('héllö')
+ 'hello'
+ >>> unaccent('HÉLLÖ')
+ 'HELLO'
+ """
+ return unicodedata.normalize('NFKD', text).\
+ encode('ascii', 'ignore').\
+ decode('utf-8')
+
+
+if __name__ == "__main__":
+ import doctest
+ doctest.testmod()