I’ve been diving deep into the world of Morse code lately and stumbled across an interesting challenge that I thought would be fun to share! So, here’s the scenario: you get a string of Morse code, which consists of dots and dashes, and you need to decode it into plain text.
Now, the catch is that I want you to do this in the fewest characters of code possible. I’ve seen a variety of ways to tackle this, but I’m looking for the most succinct and clever method you can come up with!
The Morse code representation is simple: each letter corresponds to a unique combination of dots (.) and dashes (-). For example, A is “.-“, B is “-…”, etc. You can find the full chart online or in any Morse code reference.
To make things a bit more interesting, let’s add a twist. You have to consider spaces between letters and between words. A single space (‘ ‘) should separate letters, while a slash (‘/’) should be used to denote the gap between words. So if we were to decode “.- / -…” that should translate to “A B”.
Here’s an example of what the input and output could look like:
Input:
“`
…. . .-.. .-.. — / .– — .-. .-.. -..
“`
Expected output:
“`
HELLO WORLD
“`
The real challenge lies in keeping your code as compact as possible. It’s easy to overthink it and end up writing something lengthy, but I’ve seen some incredibly compact solutions that pack a punch.
I’d love to see how you all approach this and what creative code snippets you come up with. Do you have any shortcuts or specific techniques that you think would help minimize character count? I know some of you are masters at this kind of thing, so feel free to share your thought processes and solutions. Let’s get decoding!
2 Answers