This is my assignment:
develop a program to translate messages into Morse Code, and some letters of the messages were corrupted.
To test your program, you were given a few sentences in morse code. These sentences do not contain numbers or punctuation. Letters are separated by one space and words are separated by two spaces. Also, some letters, which are corrupted, will be terminated by a * character. Letters in Morse Code will be represented by the characters . and - according to the following table:
Your goal will be to print the translated sentence using capital letters in place of the Morse Code symbols. In your final translation, you must remove spaces between letters and leave only one space between words. For corrupted letters, you must adopt special treatment. The problem with these letters is that they may have lost part of their suffix in transmission, ie some of the symbols at the end of the Morse Code representation may be missing. To translate these letters you must put all the letters that can represent the given code in square brackets. For example, the .-* code can represent the letters A (.-), J (.---), L (.-..), P (.--.), R (.-.) or W (.--), so it must be represented by [AJLPRW]. So the word .- -- --- .-* will be translated as AMO[AJLPRW]. To avoid multiple representations, the letters in square brackets must be in alphabetical order.
I have super basic level python, don't know much
error:
if mensagem[letra] == alfabeto.values() :
~~~~~~~~^^^^^^^
TypeError: list indices must be integers or slices, not str
(which I don't understant)
# Dicionário com o alfabeto em código morse
alfabeto = {
'.-': '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', '': ' '}
mensagem = [] #your inital message
mensagem.append(str(input()))
mensagem.append(' ')
texto = [] #each letter
frase = [] #final text translated
l_corromp = [] #corrupted letter
corromp = [] #corrupted letter alternatives
for letra in mensagem:
if '*' in letra:
l_corromp.append(letra)
letra = letra[:-1]
corromp.append(letra)
if letra in corromp:
corromp[letra] = alfabeto.values()
if mensagem[letra] == alfabeto.values() :
texto.append(alfabeto.keys())
if mensagem[letra] == ' ':
texto.append(' ')
print(texto)
# Impressão da mensagem traduzida
print('Mensagem traduzida: ', frase)
You can take an inductive approach. First write a function that can accept a string with a Morse code letter and return the corresponding English letter. For uncorrupted letters this is just a simple lookup. Corrupted letters require additional treatment:
Now you can use this function to translate a word:
Now you can use a similar technique to combine the words: