CodeWars Kata: Decode the Morse code, advanced

 • 

I like solving programming exercises sometimes when I have free time. It is fun and it helps keeps the "programming problem solving brain", particularly as day to day management work gets further away from programming.

This time I solved a nice Kata called "Decode Morse, advanced" from the great site called CodeWars. I wanted to put it here to preserve the solution for posterity.

Usually, these type of exercises have a "gotcha" element. The one-thing that you have to solve in order to implement it. In this case is finding the rate of the message. After a couple of approaches, the one in the code is the one I liked the most.

This is my final code:

var decodeBits = function(bits){

    // remove trailing 0s from the beginning and the end

    bits = bits.replace(/^0+/,'').replace(/0+$/,'')

    // find the rate by getting the "minimum" cluster of 1s or 0s, corresponding to a 'dot'    
    let rate1s =  bits.split('0').map(c => c.length).filter(c=> c!=0).sort((a,b)=>{return a - b})[0]
    let rate0s =  bits.split('1').map(c => c.length).filter(c=> c!=0).sort((a,b)=>{return a - b})[0] ||  rate1s
    let rate = Math.min(rate1s , rate0s);
    // replace pauses between words
    let reg = new RegExp("0".repeat(rate*7),"g");
    let data = bits.replace(reg," ");

    // replace pauses between characters
    reg = new RegExp("0".repeat(rate*3),"g");
    data = data.replace(reg,'|')    
    let words = data.split(" ")
    let output = []
    words.forEach(word => {
     let wordOutput = []
     word.split('|').forEach(letter => {
       // get dashes
       let reg = new RegExp("1".repeat(rate*3),"g");  
       letter = letter.replace(reg,'-');
       // get dots
       reg = new RegExp("1".repeat(rate*1),"g");
       letter = letter.replace(reg,'.')

       // remove 0s
       letter = letter.replace(/0/g,'')
       wordOutput.push(letter)
     });
     output.push(wordOutput);         

    })
    return output;
}

var decodeMorse = function(morseCode){  
  return morseCode.map(word => {
    return word.map(letter => MORSE_CODE[letter]).join('')
  }).join(' ');
}

Some tests:

Test.describe("Example from description", function(){  
Test.assertEquals(decodeMorse(decodeBits('1100110011001100000011000000111111001100111111001111110000000000000011001111110011111100111111000000110011001111110000001111110011001100000011')), 'HEY JUDE')  
Test.assertEquals(decodeMorse(decodeBits('11111111111111100000000000000011111000001111100000111110000011111000000000000000111110000000000000000000000000000000000011111111111111100000111111111111111000001111100000111111111111111000000000000000111110000011111000001111111111111110000000000000001111100000111110000000000000001111111111111110000011111000001111111111111110000011111000000000000000111111111111111000001111100000111111111111111000000000000000000000000000000000001111111111111110000011111000001111100000111110000000000000001111100000111111111111111000001111100000000000000011111111111111100000111111111111111000001111111111111110000000000000001111100000111111111111111000001111111111111110000000000000001111111111111110000011111000000000000000000000000000000000001111100000111110000011111111111111100000111110000000000000001111111111111110000011111111111111100000111111111111111000000000000000111111111111111000001111100000111110000011111111111111100000000000000000000000000000000000111110000011111111111111100000111111111111111000001111111111111110000000000000001111100000111110000011111111111111100000000000000011111111111111100000111111111111111000000000000000111110000011111111111111100000111111111111111000001111100000000000000011111000001111100000111110000000000000000000000000000000000011111111111111100000111111111111111000001111111111111110000000000000001111100000111110000011111000001111111111111110000000000000001111100000000000000011111000001111111111111110000011111000000000000000000000000000000000001111111111111110000000000000001111100000111110000011111000001111100000000000000011111000000000000000000000000000000000001111100000111111111111111000001111100000111110000000000000001111100000111111111111111000000000000000111111111111111000001111111111111110000011111000001111100000000000000011111111111111100000111110000011111111111111100000111111111111111000000000000000000000000000000000001111111111111110000011111000001111100000000000000011111111111111100000111111111111111000001111111111111110000000000000001111111111111110000011111111111111100000111110000000000000001111100000111111111111111000001111100000111111111111111000001111100000111111111111111')), 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.')  
})

How would you solve it?