• 1
    Input and Output Data
    • Tasks
  • 2
    Conditions
    • Tasks
  • 3
    For Loop
    • Tasks
  • 4
    Strings
    • Tasks
  • 5
    While Loop
    • Tasks
  • 6
    Lists
    • Tasks
  • 7
    Two-Dimensional Arrays
    • Tasks
  • 8
    Dictionaries
    • Tasks
  • 9
    Sets
    • Tasks
  • 10
    Functions and Recursion
    • Tasks
  • к

Lesson 1. Entering and output of data

Difficulty level:

Task«Morze code»

💻 Python

Morza code & mdash; This is a way of coding symbols using sequences of points and dash. Each letter, figure or punctuation mark has its own unique code.

rn

.... .. . And the message "Hey Jude" & mdash; & nbsp; ..... --.----- ..--... .

rn

there is also a special code for the disaster signal SOS, which is transmitted Superior: & nbsp; & middot; & middot; & middot; & minus; & minus; & minus; & middot; & middot; & middot; . This code should be processed as a single indivisible symbol "SOS".

rn

your task & mdash; Write a program that takes a line with the Morse code to the input and decodes it into a text that is understandable to a person. All letters in the final message should be title. Additional gaps at the beginning or at the end of the input line should be ignored.

Input format

One line containing a message on the Morse code. Symbols & nbsp; .

Output format

One line & mdash; Decoded message in the upper register in English.

Example

Input

..... --.-- .--- ..--...

Output

hey jude

Hint

There will be no clue here, decide for yourself!

main.py
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Developer’s solution
# Словарь для сопоставления кодов Морзе и символов.
# Включает специальный код для SOS.
MORSE_CODE_DICT = { '.-':'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', '-----':'0', '.----':'1', '..---':'2', 
                    '...--':'3', '....-':'4', '.....':'5', '-....':'6', '--...':'7', 
                    '---..':'8', '----.':'9', '···−−−···':'SOS' }

def decode_morse(morse_code: str) -> str:
    """
    Декодирует строку с кодом Морзе в человекочитаемый текст.
    
    :param morse_code: Строка с кодом Морзе.
    :return: Декодированная строка.
    """
    # Удаляем лишние пробелы в начале и конце строки
    morse_code = morse_code.strip()
    
    decoded_message = []
    # Разделяем сообщение на слова по трём пробелам
    morse_words = morse_code.split('   ')
    
    for morse_word in morse_words:
        decoded_word = ''
        # Разделяем слово на коды букв по одному пробелу
        morse_letters = morse_word.split(' ')
        for letter_code in morse_letters:
            # Находим соответствующий символ в словаре
            # Используем get для избежания ошибки, если код не найден
            decoded_word += MORSE_CODE_DICT.get(letter_code, '')
        decoded_message.append(decoded_word)
        
    # Соединяем декодированные слова в одно предложение
    return ' '.join(decoded_message)

# Пример использования из условия
input_morse = input()
print(decode_morse(input_morse))

🎉 Congratulations! 🎉

You did an excellent job with the task! It was a challenging problem, but you found the correct solution. You are one step closer to mastering programming! Keep up the good work, because every stage you pass makes you even stronger.

AD

Advertisement

red-snake blue-snake green-snake

Running your code...

AI Helper

Hi! I am your programming assistant. Ask me anything about Python — I can explain functions, methods, and help you understand the current task!