-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodificador
95 lines (86 loc) · 2.82 KB
/
codificador
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def sum_position(char):
return ord(char.lower()) - ord('a') + 1
def position_to_char(position):
return chr((position - 1) % 26 + ord('a'))
def encode_system_1(text):
encoded_text = ''
for char in text:
if char.isalpha():
position = sum_position(char)
new_position = (position + 3) % 26
if new_position == 0:
new_position = 26
new_char = position_to_char(new_position)
encoded_text += new_char
else:
encoded_text += char
return encoded_text
def decode_system_1(text):
decoded_text = ''
for char in text:
if char.isalpha():
position = sum_position(char)
new_position = (position - 3) % 26
if new_position <= 0:
new_position += 26
new_char = position_to_char(new_position)
decoded_text += new_char
else:
decoded_text += char
return decoded_text
def encode_system_2(text):
encoded_text = ''
for char in text:
if char.isalpha():
position = sum_position(char)
new_position = (position * 2) % 26
if new_position == 0:
new_position = 26
new_char = position_to_char(new_position)
encoded_text += new_char
else:
encoded_text += char
return encoded_text
def decode_system_2(text):
decoded_text = ''
for char in text:
if char.isalpha():
position = sum_position(char)
# Find the modular inverse of 2 mod 26, which is 13 (because 2 * 13 ≡ 1 mod 26)
inv = 13
new_position = (position * inv) % 26
if new_position == 0:
new_position = 26
new_char = position_to_char(new_position)
decoded_text += new_char
else:
decoded_text += char
return decoded_text
def main_menu():
while True:
print("\nBienvenida al juego")
print("opciones:")
print("1. Codifica en codigo 1")
print("2. Decodifica en codigo 1")
print("3. Codificar en codigo 2")
print("4. Decodificar en codigo 2")
print("5. Atras")
option = input("di opcion: ")
if option in ['1', '2', '3', '4']:
text = input("Di la frase: ")
if option == '1':
result = encode_system_1(text)
elif option == '2':
result = decode_system_1(text)
elif option == '3':
result = encode_system_2(text)
elif option == '4':
result = decode_system_2(text)
print("resultado:", result)
elif option == '5':
print("fin")
break
else:
print("invalido")
if __name__ == "__main__":
main_menu()