본문 바로가기
코테연습

34.Decode the Morse code

by hxunz 2022. 4. 8.

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"

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:

  • Coffeescript/C++/Go/JavaScript/Julia/PHP/Python/Ruby/TypeScript: MORSE_CODE['.--']
  • C#: MorseCode.Get(".--") (returns string)
  • F#: 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
  • Haskell: morseCodes ! ".--" (Codes are in a Map String String)
  • Java: MorseCode.get(".--")
  • Kotlin: MorseCode[".--"] ?: "" or MorseCode.getOrDefault(".--", "")
  • Racket: morse-code (a hash table)
  • Rust: MORSE_CODE
  • Scala: morseCodes(".--")
  • Swift: MorseCode[".--"] ?? "" or MorseCode[".--", default: ""]
  • C: provides parallel arrays, i.e. morse[2] == "-.-" for ascii[2] == "C"
  • NASM: a table of pointers to the morsecodes, and a corresponding list of ascii symbols

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.

 


decodeMorse = function (morseCode) {
  const code = morseCode.split("   ");
  const alphabet = code.map(word => word.split(' ').map(w => MORSE_CODE[w]).join(''));
  
  return alphabet.join(' ').trim();
}

먼저, 주어진 모스부호를 문장으로 나타내기 위해서 3칸 공백이 있는 단어들을 나누어주었다.

const code = morseCode.split(" ");

그 다음에 모스부호를 알파벳으로 바꾸기 위해서 공백 기준으로 한번 더 split() 해주고

map을 사용해서 주어진 MORSE_CODE 에서 모스부호에 맞는 알파벳을 찾아서 새로운 배열로 나타내주었다. 

const alphabet = code.map(word => word.split(' ').map(w => MORSE_CODE[w]).join(''));

그 다음에는 공백을 추가해서 string으로 나타내주었다. 

이때 앞 뒤에 공백이 있는 경우에 에러가 났다. 그래서 trim()을 사용해서 앞 뒤 공백을 제거해주었다. 

return alphabet.join(' ').trim();

 

'코테연습' 카테고리의 다른 글

36.Does my number look big in this?  (0) 2022.04.10
35.Unique In Order  (0) 2022.04.09
33.Tribonacci Sequence  (0) 2022.04.07
32.Your order, please  (0) 2022.04.07
31.Replace With Alphabet Position  (0) 2022.04.07

댓글