Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #24: Python #3830

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Retos/Reto #24 - CIFRADO CÉSAR [Fácil]/python/klyone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3

alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def are_arguments_valid(text, n):
if n == 0 or abs(n) >= len(alphabet):
return False
else:
return True

def prepare_text(text):
return text.upper()

def translate_text(text, jumps):
translate = ""

for t in text:
if t in alphabet:
pos = alphabet.find(t)
pos = (pos + jumps) % len(alphabet)
t = alphabet[pos]

translate += t
return translate

def encode_cesar(text, n):
if not are_arguments_valid(text, n):
return text

text = prepare_text(text)
return translate_text(text, n)

def decode_cesar(text, n):
return encode_cesar(text, -n)

if __name__ == "__main__":
print(encode_cesar("HELLO WORLD", 2))
print(decode_cesar(encode_cesar("NICE ONE", 5), 5))
print(encode_cesar("ThiS Is A WonderFULl PLaCE", 17))