--- id: 6a22d77ddf034bc4e35b1d5c title: "Challenge 355: Morse Code" challengeType: 29 dashedName: challenge-355 --- # --description-- Given a Morse code string, return the decoded message using the following table: | Code | Letter | Code | Letter | |------|--------|------|--------| | `.-` | `A` | `-.` | `N` | | `-...` | `B` | `---` | `O` | | `-.-.` | `C` | `.--.` | `P` | | `-..` | `D` | `--.-` | `Q` | | `.` | `E` | `.-.` | `R` | | `..-.` | `F` | `...` | `S` | | `--.` | `G` | `-` | `T` | | `....` | `H` | `..-` | `U` | | `..` | `I` | `...-` | `V` | | `.---` | `J` | `.--` | `W` | | `-.-` | `K` | `-..-` | `X` | | `.-..` | `L` | `-.--` | `Y` | | `--` | `M` | `--..` | `Z` | - Letters are separated by a single space - Words are separated by three spaces # --hints-- `decode_morse("--..")` should return `"Z"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_morse("--.."), "Z")`) }}) ``` `decode_morse("... --- ...")` should return `"SOS"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_morse("... --- ..."), "SOS")`) }}) ``` `decode_morse("..-. .-. . . -.-. --- -.. . -.-. .- -- .--.")` should return `"FREECODECAMP"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_morse("..-. .-. . . -.-. --- -.. . -.-. .- -- .--."), "FREECODECAMP")`) }}) ``` `decode_morse(".... . .-.. .-.. --- .-- --- .-. .-.. -..")` should return `"HELLO WORLD"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_morse(".... . .-.. .-.. --- .-- --- .-. .-.. -.."), "HELLO WORLD")`) }}) ``` `decode_morse("- .... . --.- ..- .. -.-. -.- -... .-. --- .-- -. ..-. --- -..- .--- ..- -- .--. . -.. --- ...- . .-. - .... . .-.. .- --.. -.-- -.. --- --.")` should return `"THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_morse("- .... . --.- ..- .. -.-. -.- -... .-. --- .-- -. ..-. --- -..- .--- ..- -- .--. . -.. --- ...- . .-. - .... . .-.. .- --.. -.-- -.. --- --."), "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG")`) }}) ``` # --seed-- ## --seed-contents-- ```py def decode_morse(code): return code ``` # --solutions-- ```py def decode_morse(code): table = { '.-': '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' } return ' '.join( ''.join(table[c] for c in word.split(' ')) for word in code.split(' ') ) ```