From 626036b784bcd3c55d150e5d92e9e704a0249b7a Mon Sep 17 00:00:00 2001 From: Marco Torres Date: Fri, 22 Sep 2023 23:10:14 -0700 Subject: [PATCH] Reto #24 - Python --- .../python/marcoatrs.py" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/python/marcoatrs.py" diff --git "a/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/python/marcoatrs.py" "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/python/marcoatrs.py" new file mode 100644 index 0000000000..6fda044c5e --- /dev/null +++ "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/python/marcoatrs.py" @@ -0,0 +1,32 @@ +from string import ascii_letters + +def normal_a_cesar(texto: str, desplazamiento: int) -> str: + cifrado = [] + for t in texto: + if t not in ascii_letters: + cifrado.append(t) + continue + limits = (65, 90) if t == t.capitalize() else (97, 122) + num = (ord(t) + desplazamiento) + if num > limits[1]: + num = (num - limits[1] + 1) + limits[0] + cifrado.append(chr(num)) + return "".join(cifrado) + + +def cesar_a_normal(texto: str, desplazamiento: int): + normal = [] + for t in texto: + if t not in ascii_letters: + normal.append(t) + continue + limits = (65, 90) if t == t.capitalize() else (97, 122) + num = (ord(t) - desplazamiento) + if num < limits[0]: + num = limits[1] - limits[0] - num + normal.append(chr(num)) + return "".join(normal) + + +print(normal_a_cesar("Hola, no me entiendes muajaja!", 3)) +print(cesar_a_normal(normal_a_cesar("Hola, no me entiendes muajaja!", 3), 3)) \ No newline at end of file