Skip to content

Instantly share code, notes, and snippets.

@dr1nk0rdi3
Last active July 30, 2019 12:03
Show Gist options
  • Save dr1nk0rdi3/27782a0015b929fdec7889cf3ed0a809 to your computer and use it in GitHub Desktop.
Save dr1nk0rdi3/27782a0015b929fdec7889cf3ed0a809 to your computer and use it in GitHub Desktop.
TTD com Python, Cifra de Cesar
from string import ascii_uppercase
def rottext():
alfabeto,rot = ascii_uppercase, 3
alfabeto_rotacionado = alfabeto[rot:] + alfabeto[:rot]
return alfabeto, alfabeto_rotacionado
def cesar_encrypt(plain_text: str) -> str:
alf, alf_rot = rottext()
tabela = str.maketrans(alf,alf_rot)
return plain_text.translate(tabela)
def cesar_decrypt(plain_text: str) -> str:
alf, alf_rot = rottext()
tabela = str.maketrans(alf_rot,alf)
return plain_text.translate(tabela)
def verifica_alfa(plain_text) -> bool:
return plain_text.isalpha()
from unittest import TestCase, main
from cifracesar import rottext, cesar_encrypt, cesar_decrypt, verifica_alfa
from string import ascii_uppercase
class TestCifraCesar(TestCase):
def test_rotacao_alfabeto(self):
"""
rot = 3
alfabeto = 'ABCDE'
afabeto_rotacionado = 'DEABC'
"""
alfa_esperado_rot3 = 'DEFGHIJKLMNOPQRSTUVWXYZABC'
self.assertEqual(alfa_esperado_rot3,rottext()[1])
def test_encrypt_cesar(self):
"""
rot = 3
plain_text = 'LOL'
text_cifra = 'ORO'
"""
text_cifra_esperado = 'YDQGUH'
self.assertEqual(text_cifra_esperado, cesar_encrypt('VANDRE'))
def test_decrypt_cesar(self):
"""
rot = 3
plain_text = 'ORO'
text_decifra = 'LOL'
"""
text_decifra_esperado = 'VANDRE'
self.assertEqual(text_decifra_esperado, cesar_decrypt('YDQGUH'))
def test_se_entrada_somente_text(self):
"""
não permite caracteres diferentes de 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
text_nao_esperado = '1ASWQ'
"""
text_nao_alfabeto = True
self.assertEqual(text_nao_alfabeto, verifica_alfa('1ASWQ'))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment