Using the power of programming, we've translated Pig Latin and told humans the time. Now for this challenge, you have to write a simple Morse code decoder. Today's challenge comes from user jolaf on CodeWars
Your task is to implement a function that would take the Morse code as input and return a decoded human-readable string.
For example:
decodeMorse('.... . -.-- .--- ..- -.. .')
should return"HEY JUDE"
C++/Go/JavaScript/PHP/Python/Ruby/TypeScript:
MORSE_CODE['.--']
C#:MorseCode.Get(".--") (returns string)
Elixir:morse_codes variable
Elm:MorseCodes.get : Dict String String
Haskell:morseCodes ! ".--" (Codes are in a Map String String)
Java:MorseCode.get(".--")
Kotlin:MorseCode[".--"] ?: "" or MorseCode.getOrDefault(".--", "")
Rust:self.morse_code
Scala:morseCodes(".--")
NOTE: For coding purposes, you should use ASCII characters .
and -
, not Unicode characters.
I always thought it would be really cool to make a full-size Morse code decoder, taking audio as input and reading aloud the output. Perhaps I could start building it as a side project - it would give me an excuse to buy another Raspberry Pi.
Good luck, happy coding!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (15)
JS does not have any builtin morse library, therefore we need to build it in order to use a simple oneliner:
And the result is:
And here goes the encoder, using the same
m
variable as aboveThe encoder produces the following result:
Rust Solution!
Decided to make functions that can go in both directions! So we can both encode and decode worse code!
It's gonna just panic on invalid input at the moment, but it should probably return a Result type but I was feeling lazy in this department
Meta: @thepracticaldev Might want to remove the
Morse code tables are already preloaded for many languages as a dictionary, feel free to use them:
bit since I think that only applies to the CodeWars solution runner, so isn't as relevant here!
Cause for instance Rust definitely does NOT just have a morse code map available at
self.morse_code
Great point! Going to make the fix here shortly. Thanks for the heads up!
Solved several years ago in a golf challenge. Perl (newlines added for readability):
Similar solution. Nim.
Its a one liner in C# once you have the dictionary set up:
Complete code:
Obligatory Elixir:
Am I the only one having trouble with the specified preloaded dictionaries?
I'm pretty sure this an aggressive copy pasta from the CodeWars site.
Code wars has a solution runner, and I think these are defined in their specific runner. Cause the Rust example is definitely not something that is just globally available in Rust lol
Here is the Codewars link if you want to try it out. I didn't give it a shot so no idea how well it works: codewars.com/kata/decode-the-morse...
That's half an hour of my life I won't get back.
I should have thought about that sooner :-/
Ruby Language
The problem definition was initially tripping because it appeared to somehow render "HEY JUDE" without some sort of demarcation that indicated a space between words since HTML rendering removes extra spacing. However, opening and viewing the HTML source revealed there was indeed multiple spaces between the words. In this implementation, two or more spaces represents a "pause" which is the morse standard for recognizing word breaks.
Code with Specs
output
Python solution: