前提・実現したいこと
CheckioというサイトでMorse Decoderという問題を解いています。
問題文
(Your task is to decrypt the secret message using the Morse code.
The message will consist of words with 3 spaces between them and 1 space between each letter of each word.
If the decrypted text starts with a letter then you'll have to print this letter in uppercase. )
モールス信号を文字列にして出力するというものなのですが、単語間にスペースを入れることが出来ません。
発生している問題・エラーメッセージ
"... --- -- . - . -..- -" Your result "Sometext" Right result "Some text"
該当のソースコード
Python
1MORSE = {'.-': 'a', '-...': 'b', '-.-.': 'c', 2 '-..': 'd', '.': 'e', '..-.': 'f', 3 '--.': 'g', '....': 'h', '..': 'i', 4 '.---': 'j', '-.-': 'k', '.-..': 'l', 5 '--': 'm', '-.': 'n', '---': 'o', 6 '.--.': 'p', '--.-': 'q', '.-.': 'r', 7 '...': 's', '-': 't', '..-': 'u', 8 '...-': 'v', '.--': 'w', '-..-': 'x', 9 '-.--': 'y', '--..': 'z', '-----': '0', 10 '.----': '1', '..---': '2', '...--': '3', 11 '....-': '4', '.....': '5', '-....': '6', 12 '--...': '7', '---..': '8', '----.': '9' 13 } 14 15def morse_decoder(code): 16 #replace this for solution 17 result = "" 18 words = code.split(" ") 19 moji = code.split(" ") 20 for morse in moji: 21 if morse in moji: 22 result += MORSE.get(morse, str(morse)) 23 if len(result) == 1: 24 result = result.upper() 25 return result 26 27 28if __name__ == '__main__': 29 print("Example:") 30 print(morse_decoder('... --- ...')) 31 32 #These "asserts" using only for self-checking and not necessary for auto-testing 33 assert morse_decoder("... --- -- . - . -..- -") == "Some text" 34 assert morse_decoder("..--- ----- .---- ---..") == "2018" 35 assert morse_decoder(".. - .-- .- ... .- --. --- --- -.. -.. .- -.--") == "It was a good day" 36 print("Coding complete? Click 'Check' to earn cool rewards!")
試したこと
単語ごとに分けた後、文字ごとに分け変換し足すという方法ならできると思ったのですが、うまく表現することが出来ません。
"... --- -- . - . -..- -" → ("... --- -- .", "- . -..- -")
→ (("...", "---", "--", "."), ("-", ".", "-..-", "-"))
→ ("s", "o", "m", "e"), ("t", "e", "x", "t")
→ 一単語の文字数とresultに入った文字数が同じであればスペースを入れる
といった感じで考えていました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/09/11 10:05
2018/09/11 10:10
退会済みユーザー
2018/09/11 10:21