Last active
January 6, 2022 11:47
-
-
Save sandrabosk/ca2ecc3e65a532d73ddda0d2974abaa5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ************************************************************************************ | |
// https://www.codewars.com/kata/54b724efac3d5402db00065e | |
// 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. | |
// ************************************************************************************ | |
const decodeLetter = letter => { | |
return MORSE_CODE[letter]; | |
} | |
const decodeWord = word => { | |
return word.split(' ').map(decodeLetter).join(''); | |
} | |
const decodeMorse = morseCode => { | |
return morseCode.trim().split(' ').map(decodeWord).join(' '); | |
} | |
// helper - this is preloaded Morse code: | |
const MORSE_CODE = { | |
'.-': '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', | |
'-----': '0', | |
'.----': '1', | |
'..---': '2', | |
'…--': '3', | |
'….-': '4', | |
'…..': '5', | |
'-….': '6', | |
'--…': '7', | |
'---..': '8', | |
'----.': '9', | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Took me a minute to decode those map functions but I get it.
👍