6 kyu

Decode the Morse code

39,997 of 122,696jolaf

Description:

Part of Series 1/3
This kata is part of a series on the Morse code. After you solve this kata, you may move to the next one.

In this kata you have to write a simple Morse code decoder. While the Morse code is now mostly superseded by voice and digital data communication channels, it still has its use in some applications around the world.

The Morse code encodes every character as a sequence of "dots" and "dashes". For example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded as ·−−−−. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. For example, the message HEY JUDE in Morse code is ···· · −·−−   ·−−− ··− −·· ·.

NOTE: Extra spaces before or after the code have no meaning and should be ignored.

In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···−−−···. These special codes are treated as single special characters, and usually are transmitted as separate words.

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"
decodeMorse('.... . -.--   .--- ..- -.. .')
//should return "HEY JUDE"
MorseCodeDecoder.Decode(".... . -.--   .--- ..- -.. .")
//should return "HEY JUDE"
decodeMorse ".... . -.--   .--- ..- -.. ."
// should return "HEY JUDE"
MorseCode.decode('.... . -.--   .--- ..- -.. .')
#=> "HEY JUDE"
MorseCode.decode ".... . -.--   .--- ..- -.. ."
--should return "HEY JUDE"
DecodeMorse(".... . -.--   .--- ..- -.. .")
// should return "HEY JUDE"
decodeMorse ".... . -.--   .--- ..- -.. ."
--should return "HEY JUDE"
MorseCodeDecoder.decode(".... . -.--   .--- ..- -.. .")
//should return "HEY JUDE"
decodeMorse('.... . -.--   .--- ..- -.. .')
//should return "HEY JUDE"
decodeMorse('.... . -.--   .--- ..- -.. .')
//should return "HEY JUDE"
decode_morse('.... . -.--   .--- ..- -.. .')
//should return "HEY JUDE"
decode_morse('.... . -.--   .--- ..- -.. .')
#should return "HEY JUDE"
(decode-morse ".... . -.--   .--- ..- -.. .")
; should return "HEY JUDE"
decodeMorse('.... . -.--   .--- ..- -.. .')
#should return "HEY JUDE"
decodeMorse('.... . -.--   .--- ..- -.. .')
//should return "HEY JUDE"
decodeMorse('.... . -.--   .--- ..- -.. .')
//should return "HEY JUDE"
decode_morse(".... . -.--   .--- ..- -.. .")
//should return "HEY JUDE"
MorseDecoder.decode(".... . -.--   .--- ..- -.. .")
//should return "HEY JUDE"
decode_morse(".... . -.--   .--- ..- -.. .")
// should return "HEY JUDE"
decodemorse(".... . -.--   .--- ..- -.. .")
# should return "HEY JUDE"
a call to decode_morse with RDI set to the address of ".... . -.--   .--- ..- -.. ."
should fill the buffer pointed to by RDI with db 'HEY JUDE',0
Kata.decodemorse(".... . -.--   .--- ..- -.. .")
// should return "HEY JUDE"

NOTE: For coding purposes you have to use ASCII characters . and -, not Unicode characters.

The Morse code table is preloaded for you as a dictionary, feel free to use it:

  • C: provides parallel arrays, i.e. morse[2] == "-.-" for ascii[2] == "C"
  • Coffeescript/C++/Go/JavaScript/Julia/PHP/Python/Ruby/TypeScript/Groovy: MORSE_CODE['.--']
  • C#: MorseCode.Get(".--") (returns string)
  • Elixir: @morse_codes variable (from use MorseCode.Constants). Ignore the unused variable warning for morse_codes because it's no longer used and kept only for old solutions.
  • Elm: MorseCodes.get : Dict String String
  • F#: MorseCode.get ".--" (returns string)
  • Haskell: morseCodes ! ".--" (Codes are in a Map String String)
  • Java: MorseCode.get(".--")
  • Kotlin: MorseCode[".--"] ?: "" or MorseCode.getOrDefault(".--", "")
  • NASM: a table of pointers to the morsecodes, and a corresponding list of ascii symbols
  • Racket: morse-code (a hash table)
  • Rust: MORSE_CODE
  • Scala: morseCodes(".--")
  • Swift: MorseCode[".--"] ?? "" or MorseCode[".--", default: ""]

All the test strings would contain valid Morse code, so you may skip checking for errors and exceptions. In C#, tests will fail if the solution code throws an exception, please keep that in mind. This is mostly because otherwise the engine would simply ignore the tests, resulting in a "valid" solution.

Good luck!

After you complete this kata, you may try yourself at Decode the Morse code, advanced.

Algorithms

Stats:

CreatedJan 15, 2015
PublishedJan 15, 2015
Warriors Trained393296
Total Skips61639
Total Code Submissions1090143
Total Times Completed122696
Python Completions39997
JavaScript Completions38290
Ruby Completions4300
Java Completions14312
C# Completions9492
TypeScript Completions3624
PHP Completions2896
Haskell Completions950
C++ Completions4122
CoffeeScript Completions36
Elixir Completions317
Go Completions2344
Rust Completions1789
Kotlin Completions1331
Scala Completions464
Elm Completions110
Lua Completions130
C Completions702
Julia Completions54
Swift Completions402
NASM Completions9
F# Completions39
Racket Completions26
Groovy Completions1
Total Stars5734
% of votes with a positive feedback rating89% of 14270
Total "Very Satisfied" Votes11340
Total "Somewhat Satisfied" Votes2632
Total "Not Satisfied" Votes298
Ad
Contributors
  • jolaf Avatar
  • jhoffner Avatar
  • wthit56 Avatar
  • user9396321 Avatar
  • coryshrmn Avatar
  • vertighel Avatar
  • Javatlacati Avatar
  • GiacomoSorbi Avatar
  • pablo.varela Avatar
  • YasminEndusa Avatar
  • brunolm Avatar
  • isqua Avatar
  • ditzy Avatar
  • Chrono79 Avatar
  • kazk Avatar
  • ryanwinchester Avatar
  • MFazio23 Avatar
  • danicat Avatar
  • pmatseykanets Avatar
  • Voile Avatar
  • mbuscemi Avatar
  • rowcased Avatar
  • Awesome A.D. Avatar
  • hobovsky Avatar
  • stellartux Avatar
  • OpenByte Avatar
  • trashy_incel Avatar
  • SunMaster Avatar
  • user8436785 Avatar
  • kamillo Avatar
  • Just4FunCoder Avatar
  • jeydo6 Avatar
  • FranzMartyn Avatar
  • benjaminzwhite Avatar
  • KayleighWasTaken Avatar
Ad