import string
import collections
-import logging
import math
from itertools import zip_longest, cycle, chain
from language_models import *
-logger = logging.getLogger(__name__)
-logger.addHandler(logging.FileHandler('cipher.log'))
-logger.setLevel(logging.WARNING)
-#logger.setLevel(logging.INFO)
-#logger.setLevel(logging.DEBUG)
-
modular_division_table = [[0]*26 for _ in range(26)]
for a in range(26):
return list(collections.OrderedDict.fromkeys(text))
-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)
def caesar_decipher(message, shift):
- """Encipher a message with the Caesar cipher of given shift
+ """Decipher a message with the Caesar cipher of given shift
>>> caesar_decipher('bcd', 1)
'abc'
'abc'
>>> caesar_decipher('cd ez ab', 2)
'ab cx yz'
+ >>> caesar_decipher('Jgnnq Yqtnf!', 2)
+ 'Hello World!'
"""
return caesar_encipher(message, -shift)
-def affine_encipher_letter(letter, multiplier=1, adder=0, one_based=True):
+def affine_encipher_letter(accented_letter, multiplier=1, adder=0, one_based=True):
"""Encipher a letter, given a multiplier and adder
>>> ''.join([affine_encipher_letter(l, 3, 5, True) \
for l in string.ascii_uppercase])
'FILORUXADGJMPSVYBEHKNQTWZC'
"""
+ letter = unaccent(accented_letter)
if letter in string.ascii_letters:
if letter in string.ascii_uppercase:
alphabet_start = ord('A')
"""
cipher_alphabet = keyword_cipher_alphabet_of(keyword, wrap_alphabet)
cipher_translation = ''.maketrans(string.ascii_lowercase, cipher_alphabet)
- return message.lower().translate(cipher_translation)
+ return unaccent(message).lower().translate(cipher_translation)
def keyword_decipher(message, keyword, wrap_alphabet=0):
"""Deciphers a message with a keyword substitution cipher.