Avatar of brunnock

brunnock's solution

to Run Length Encoding in the JavaScript Track

Published at Nov 20 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Implement run-length encoding and decoding.

Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count.

For example we can represent the original 53 characters with only 13.

"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"  ->  "12WB12W3B24WB"

RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.

"AABCCCDEEEE"  ->  "2AB3CD4E"  ->  "AABCCCDEEEE"

For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace. This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character.

Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine run-length-encoding.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

Source

Wikipedia https://en.wikipedia.org/wiki/Run-length_encoding

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

run-length-encoding.spec.js

var RLE = require('./run-length-encoding');

describe('Run-length encoding', function () {
  it('encode empty string', function () {
    expect(RLE.encode('')).toEqual('');
  });

  xit('encode single characters only', function () {
    expect(RLE.encode('XYZ')).toEqual('XYZ');
  });

  xit('decode empty string', function () {
    expect(RLE.decode('')).toEqual('');
  });

  xit('decode single characters only', function () {
    expect(RLE.decode('XYZ')).toEqual('XYZ');
  });

  xit('encode simple', function () {
    expect(RLE.encode('AABBBCCCC')).toEqual('2A3B4C');
  });

  xit('decode simple', function () {
    expect(RLE.decode('2A3B4C')).toEqual('AABBBCCCC');
  });

  xit('encode with single values', function () {
    expect(RLE.encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB')).toEqual('12WB12W3B24WB');
  });

  xit('decode with single values', function () {
    expect(RLE.decode('12WB12W3B24WB')).toEqual('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB');
  });

  xit('decode(encode(...))combination', function () {
    expect(RLE.decode(RLE.encode('zzz ZZ  zZ'))).toEqual('zzz ZZ  zZ');
  });
});
const RLE = {}

RLE.encode = function (str) {
  let strarr = str.split('');
  let num=1;
  let char=strarr[0];
  let code='';
  for (let x=1; x<=strarr.length; ++x) {
    if (strarr[x] === char) {
      ++num;
    } else {
      if (num>1) code += num;
      code += char;
      char = strarr[x];
      num=1;
    }
  }
  return(code);
}

const numCharExp = /(\d+\D)/;

RLE.decode = function (str) {
  return (str.split(numCharExp).map(x=>{
    if (numCharExp.test(x)) {
      let term = x.split(/(\d+)/);
      return (term[2].repeat(term[1]));
    } else {
      return(x)
    }
  }).join(''));
}

module.exports = RLE;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?