76 lines
3.0 KiB
Python
76 lines
3.0 KiB
Python
from common.common import clear
|
|
|
|
def fin_01():
|
|
"""A shift code is where a message can be easily encoded and is one of the
|
|
simplest codes to use. Each letter is moved forwards through the alphabet
|
|
a set number of letters to be represented by a new letter. For instance,
|
|
'abc' becomes 'bcd' when the code is shifted by one (i.e. each letter in
|
|
the alphabet is moved forward one character). You need to create a
|
|
program which will display the following menu:
|
|
1) Make a code
|
|
2) Decode a message
|
|
3) Quit
|
|
|
|
Enter your selection:
|
|
If the user selects 1, they should be able to type in a message
|
|
(including spaces) and then enter a number. Python should then display
|
|
the encoded message once the shift code has been applied. If the user
|
|
selects 2, they should enter an encoded message and the correct number
|
|
and it should display the decoded message (i.e. move the correct number
|
|
of letters backwards through the alphabet). If they select 3 it should
|
|
stop the program from running. After they have encoded or decoded a
|
|
message the menu should be displayed to them again until they select quit."""
|
|
abc = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
|
'n', 'ñ', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
|
|
|
|
def code_msg(msg, steps):
|
|
coded_message = ""
|
|
for letter in msg:
|
|
if letter in abc:
|
|
ind = abc.index(letter)
|
|
ind += steps
|
|
while ind >= len(abc):
|
|
ind -= len(abc)
|
|
coded_message += abc[ind]
|
|
else:
|
|
coded_message += letter
|
|
print(coded_message)
|
|
|
|
def decode_msg(msg, steps):
|
|
decoded_message = ""
|
|
for letter in msg:
|
|
if letter in abc:
|
|
ind = abc.index(letter)
|
|
ind -= steps
|
|
while ind < -len(abc):
|
|
ind += len(abc)
|
|
decoded_message += abc[ind]
|
|
else:
|
|
decoded_message += letter
|
|
print(decoded_message)
|
|
|
|
while True:
|
|
clear()
|
|
print("""
|
|
1) Codificar mensaje
|
|
2) Decodificar mensaje
|
|
s) Salir
|
|
""")
|
|
sel = input("Ingresa una opción: ")
|
|
match sel:
|
|
case '1':
|
|
msg = input("Ingresa el mensaje a codificar: ")
|
|
steps = int(input("Ingresa el nro. de pasos: "))
|
|
code_msg(msg, steps)
|
|
input("\nPresiona Enter para continuar\n")
|
|
case '2':
|
|
msg = input("Ingresa el mensaje a decodificar: ")
|
|
steps = int(input("Ingresa el nro. de pasos: "))
|
|
decode_msg(msg, steps)
|
|
input("\nPresiona Enter para continuar\n")
|
|
case 's':
|
|
break
|
|
case _:
|
|
print("Debes ingresar una opción válida")
|
|
input("\nPresiona Enter para continuar\n")
|