Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

105 lines
2.8 KiB
Markdown

---
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(' ')
)
```