-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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
59
javascript/run-length-encoding/run-length-encoding.spec.js
This file contains 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
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'); | ||
}); | ||
}); |