Skip to content

Commit

Permalink
Added Run Length Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
zhabinka committed Jan 8, 2019
1 parent 8f54e52 commit 344838c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
32 changes: 32 additions & 0 deletions javascript/run-length-encoding/run-length-encoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const encode = (str) => {
const iter = (s, arr) => {
if (s.length === 0) {
return arr;
}
const regex = new RegExp(`${s[0]}+`);
const charsGroup = s.match(regex)[0];

return iter(s.substr(charsGroup.length), arr.concat(charsGroup));
};

const groups = iter(str, []);

return groups
.map(el => (el.length === 1 ? el : `${el.length}${el[0]}`))
.join('');
};


const decode = (str) => {
if (str === '') {
return str;
}

return str
.match(/\d*.{1}/g)
.map(el => (el.length > 1 ? el : `1${el}`))
.map(el => el.slice(-1).repeat(el.match(/\d*/)))
.join('');
};

export { encode, decode };
59 changes: 59 additions & 0 deletions javascript/run-length-encoding/run-length-encoding.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { encode, decode } from './run-length-encoding';

describe('run-length encode a string', () => {
test('encode empty string', () => {
expect(encode('')).toEqual('');
});

test('single characters only are encoded without count', () => {
expect(encode('XYZ')).toEqual('XYZ');
});

test('encode string with no single characters', () => {
expect(encode('AABBBCCCC')).toEqual('2A3B4C');
});

test('encode string with single characters mixed with repeated characters', () => {
expect(encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB')).toEqual('12WB12W3B24WB');
});

test('encode string with multiple whitespaces', () => {
expect(encode(' hsqq qww ')).toEqual('2 hs2q q2w2 ');
});

test('encode string with lowercase characters', () => {
expect(encode('aabbbcccc')).toEqual('2a3b4c');
});
});

describe('run-length decode a string', () => {
test('decode empty string', () => {
expect(decode('')).toEqual('');
});

test('decode string with single characters only', () => {
expect(decode('XYZ')).toEqual('XYZ');
});

test('decode string with no single characters', () => {
expect(decode('2A3B4C')).toEqual('AABBBCCCC');
});

test('decode string with single characters mixed with repeated characters', () => {
expect(decode('12WB12W3B24WB')).toEqual('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB');
});

test('decode string with multiple whitespaces', () => {
expect(decode('2 hs2q q2w2 ')).toEqual(' hsqq qww ');
});

test('decode string with lowercase characters', () => {
expect(decode('2a3b4c')).toEqual('aabbbcccc');
});
});

describe('run-length encode and then decode', () => {
test('encode followed by decode gives original string', () => {
expect(decode(encode('zzz ZZ zZ'))).toEqual('zzz ZZ zZ');
});
});

0 comments on commit 344838c

Please sign in to comment.