Decoding a conversation

Asked

Viewed 159 times

1

Problem:

A code has been created between friends to encode conversations. There is a code relationship between letters and numbers as shown below.

inserir a descrição da imagem aqui

It is necessary to create a function called "decode", which will receive the code and returns the translated message.

Entree:

5 24 9 20 15 0 14 1 0 16 18 15 22 1
14 1 4 1 0 4 5 0 16 5 19 17 21 9 19 1
6 9 13

Note: the entry will receive several sentences until it is typed "end".

Exit

exito na prova
nada de pesquisa

My code:

lista = list(' abcdefghijklmnopqrstuvwxyz') #Quebrando a lista em índices

indice = input().split()

if indice in lista:
    print()
    #Continuação...

I did not understand very well this problem, any help is valid, thank you!

2 answers

4


You do not need to turn the code into a list, as strings can be indexed by position (the first character is at zero, the second at index 1, etc.). And since the positions of the code coincide with these indices, you can use them directly.

As for the entries, each row has several numbers, so you must iterate through the numbers and pick the character corresponding to each position:

codigo = ' abcdefghijklmnopqrstuvwxyz'
while True:
    numeros = input().split()
    palavra = ''
    for n in numeros:
        n = int(n)
        if 0 <= n < len(codigo):
            palavra += codigo[n]
    if palavra == 'fim':
        break
    print(palavra)

The while read the entries indefinitely. If the decoded word is "end", the loop ends.


It is asked to create a function to decode a word. Then it would look like this:

codigo = ' abcdefghijklmnopqrstuvwxyz'

def decodificar(numeros):
    palavra = ''
    for n in numeros:
        n = int(n)
        if 0 <= n < len(codigo):
            palavra += codigo[n]
    return palavra

while True:
    numeros = input().split()
    palavra = decodificar(numeros)
    if palavra == 'fim':
        break
    print(palavra)
  • decoficidada ? kkk

  • @Jeanextreme002 Corrected :-)

  • 1

    Thank you very much for the explanations! :)

2

As well as the another answer says, you don’t need to turn your code into a list because strings are already indexed by position, acting as a list where each character will have its index.

texto = "Hello World"
print(texto[6])       # Podemos obter o caractere de um índice: ( W )
print(texto[2:10])    # Assim como podemos utilizar slice nela: ( llo Worl ) 

To continue getting user inputs, you can use the repeat structure while which will repeat the entire code block.

One thing that got me confused is how to finish the execution of the code. In another answer, will be checked if the entry is "fim" after decoding the message, but you didn’t make that very clear in the question.

If to finish the input should be "fim" without being encoded in numbers, you cannot use the method split() directly. You will need to check the input before any action or else the message will be separated and your code may even generate an error.

while True:
    mensagem = input("Mensagem (Codificada): ")
    if mensagem.lower() == "fim": break

As you can see, to stop the replay, simply use the command break to leave the structure immediately.


To decode the message is quite simple, just split it as you did using the split and then run it in a loop for. Within the repeat structure, you must convert each input number to integer and thus get the character in your code string related to this number, as well as show the another answer.

If you want, you can create an even more simplified function that is using the method join and comprehensilist on to carry out the whole process in a single line.

See the code below:

def decodificar(numeros):
    codigo = " abcdefghijklmnopqrstuvwxyz"
    return "".join([codigo[num] if 0 <= num < len(codigo) else "" for num in numeros])

while True:
    mensagem = input("Mensagem (Codificada): ")
    if mensagem.lower() == "fim": break

    numeros = [int(num) for num in mensagem.split()]
    palavra = decodificar(numeros)
    print(palavra)
  • 1

    Thank you very much for the explanations! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.