본문 바로가기
프로그래밍 문제/[Python] CheckIO

[Check-IO] Morse Decoder

by UltraLowTemp-Physics 2021. 3. 8.
728x90

Problem: py.checkio.org/en/mission/morse-decoder/
My solution: 

MORSE = {'.-':    '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'
        }

def morse_decoder(code):
    #replace this for solution
    
    words_list = code.split('   ')   # splitting words  
    dummy = []
    for words in words_list:
        char_list = words.split(' ') # splitting characters in a word
        # de_char: decorded Morse code
        de_char = list(map(lambda x : MORSE[x], char_list))
        dummy.append(''.join(de_char)) # collect the words 
    
    return ' '.join(dummy).capitalize() # combine the words

if __name__ == '__main__':
    print("Example:")
    #print(morse_decoder('... --- ...'))

    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert morse_decoder("... --- -- .   - . -..- -") == "Some text"
    assert morse_decoder("..--- ----- .---- ---..") == "2018"
    assert morse_decoder(".. -   .-- .- ...   .-   --. --- --- -..   -.. .- -.--") == "It was a good day"
    print("Coding complete? Click 'Check' to earn cool rewards!")
728x90

'프로그래밍 문제 > [Python] CheckIO' 카테고리의 다른 글

[CheckIO] Majority  (0) 2021.03.11
[CheckIO] Backward Each Word  (0) 2021.03.10
[CheckIO] Solve All the same  (0) 2021.01.07
[CheckIO] Solve split list  (0) 2021.01.05
[CheckIO] Sun angle  (0) 2021.01.03

댓글