seg/trabalho1/cesar.py

84 lines
1.7 KiB
Python
Raw Normal View History

2023-08-22 16:29:32 +00:00
import sys
special_letters = {
"ã": "a",
"á": "a",
"à": "a",
"é": "e",
"ê": "e",
"í": "i",
"ó": "o",
"õ": "o",
"ô": "o",
"ú": "u",
"ç": "c"
}
special_chars = {
" ": " ",
".": ".",
",": ",",
";": ";",
":": ":",
"!": "!",
"?": "?",
"(": "(",
")": ")"
}
2023-08-23 12:38:41 +00:00
caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
2023-08-22 16:29:32 +00:00
def cesar(string, key):
new_string = ""
for letter in string:
if letter in special_chars:
new_string += special_chars[letter]
continue
if letter in special_letters:
letter = special_letters[letter]
2023-08-26 15:39:45 +00:00
if letter not in caracteres:
continue
2023-08-23 12:38:41 +00:00
index = caracteres.index(letter)
new_index = (index + key) % len(caracteres)
new_letter = caracteres[new_index]
2023-08-22 16:29:32 +00:00
2023-08-23 12:38:41 +00:00
new_string += new_letter
2023-08-22 16:29:32 +00:00
return new_string
2023-08-26 15:39:45 +00:00
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python3 cesar.py -c/-d [key] [string]")
sys.exit(1)
encrypt = True
if sys.argv[1] == "-d":
encrypt = False
key = int(sys.argv[2])
string = sys.argv[3]
if not encrypt:
key *= -1
print(cesar(string, key))
# # [A-Z, a-z, 0-9]
# if ord('0') <= ord(letter) <= ord('9'):
# if new_letter > ord('9'):
# new_letter += 7 # 9 + 1 = A
# elif new_letter < ord('0'):
# new_letter += 75 # 0 - 1 = z
# elif letter.isupper():
# if new_letter > ord('Z'):
# new_letter += 6 # Z + 1 = a
# elif new_letter < ord('A'):
# new_letter -= 7 # A - 1 = 9
# else:
# if new_letter > ord('z'):
# new_letter -= 75 # z + 1 = 0
# elif new_letter < ord('a'):
# new_letter -= 6 # a - 1 = Z