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
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
I'm a friendly, non-dev, cisgender guy from NC who enjoys playing music/making noise, hiking, eating veggies, and hanging out with my best friend/wife + our 3 kitties + 1 greyhound.
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
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
MORSE_CODE=Hash[*%w/
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 --.. 1 .---- 2 ..--- 3 ...-- 4 ....-
5 ..... 6 -.... 7 --... 8 ---.. 9 ----. 0 -----
/].invert.freezedefdecode_morsestrwords=str.to_s.split(/\s{2,}/)words.map{|w|w.split(" ").map{|mc|MORSE_CODE[mc]}.join}.join" "endrequire"spec"describe"#decode_morse"doit{expect(decode_morsenil).toeq""}it{expect(decode_morse"").toeq""}it{expect(decode_morse".").toeq"E"}it{expect(decode_morse". ").toeq"E"}it{expect(decode_morse"... --- ...").toeq"SOS"}it{expect(decode_morse"- . ... - .. -. --. .---- ..--- ...--").toeq"TESTING 1 2 3"}it{expect(decode_morse".... . -.-- .--- ..- -.. .").toeq"HEYJUDE"}it{expect(decode_morse".... . -.-- .--- ..- -.. .").toeq"HEY JUDE"}it{expect(decode_morse"-- --- .-. ... . -.-. --- -.. .").toeq"MORSE CODE"}end
output
>> rspec morse_code.rb
.........
Finished in 0.00551 seconds (files took 0.15391 seconds to load)
9 examples, 0 failures
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
mvariable 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
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 :-/
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_codeGreat point! Going to make the fix here shortly. Thanks for the heads up!
Obligatory Elixir:
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:
Python solution:
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
My code in c#
I think it would be a lot of fun if it was reading morse code streaming from a service.