Files
python/exercises/atbash-cipher/example.py

26 lines
527 B
Python
Raw Normal View History

from string import ascii_lowercase
import sys
if sys.version_info[0] == 2:
from string import maketrans
else:
maketrans = str.maketrans
2014-03-27 10:42:07 -03:00
BLKSZ = 5
trtbl = maketrans(ascii_lowercase, ascii_lowercase[::-1])
2014-03-27 10:42:07 -03:00
def base_trans(text):
return ''.join([c for c in text if c.isalnum()]).lower().translate(trtbl)
2014-03-27 10:42:07 -03:00
def encode(plain):
cipher = base_trans(plain)
return " ".join([cipher[i:i + BLKSZ]
for i in range(0, len(cipher), BLKSZ)])
2014-03-27 10:42:07 -03:00
def decode(ciphered):
return base_trans(ciphered)