forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Retos/Reto #24 - CIFRADO CÉSAR [Fácil]/python/marcoatrs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |