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
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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