javascript - run-length-encoding
This commit is contained in:
parent
9589d3409f
commit
c156ad6dcc
60
javascript/run-length-encoding/README.md
Normal file
60
javascript/run-length-encoding/README.md
Normal file
@ -0,0 +1,60 @@
|
||||
# Run Length Encoding
|
||||
|
||||
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.
|
||||
|
||||
```text
|
||||
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB"
|
||||
```
|
||||
|
||||
RLE allows the original data to be perfectly reconstructed from
|
||||
the compressed data, which makes it a lossless data compression.
|
||||
|
||||
```text
|
||||
"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:
|
||||
|
||||
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
|
||||
|
||||
## Requirements
|
||||
|
||||
Install assignment dependencies:
|
||||
|
||||
```bash
|
||||
$ npm install
|
||||
```
|
||||
|
||||
## Making the test suite pass
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ npm test
|
||||
```
|
||||
|
||||
In the test suites all tests but the first have been skipped.
|
||||
|
||||
Once you get a test passing, you can enable the next one by
|
||||
changing `xtest` to `test`.
|
||||
|
||||
|
||||
## Source
|
||||
|
||||
Wikipedia [https://en.wikipedia.org/wiki/Run-length_encoding](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.
|
7080
javascript/run-length-encoding/package-lock.json
generated
Normal file
7080
javascript/run-length-encoding/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
80
javascript/run-length-encoding/package.json
Normal file
80
javascript/run-length-encoding/package.json
Normal file
@ -0,0 +1,80 @@
|
||||
{
|
||||
"name": "exercism-javascript",
|
||||
"version": "0.0.0",
|
||||
"description": "Exercism exercises in Javascript.",
|
||||
"author": "Katrina Owen",
|
||||
"private": true,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/exercism/javascript"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-jest": "^21.2.0",
|
||||
"babel-plugin-transform-builtin-extend": "^1.1.2",
|
||||
"babel-preset-env": "^1.4.0",
|
||||
"eslint": "^3.19.0",
|
||||
"eslint-config-airbnb": "^15.0.1",
|
||||
"eslint-plugin-import": "^2.2.0",
|
||||
"eslint-plugin-jsx-a11y": "^5.0.1",
|
||||
"eslint-plugin-react": "^7.0.1",
|
||||
"jest": "^21.2.1"
|
||||
},
|
||||
"jest": {
|
||||
"modulePathIgnorePatterns": [
|
||||
"package.json"
|
||||
]
|
||||
},
|
||||
"babel": {
|
||||
"presets": [
|
||||
[
|
||||
"env",
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"node": "current"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
],
|
||||
"plugins": [
|
||||
[
|
||||
"babel-plugin-transform-builtin-extend",
|
||||
{
|
||||
"globals": [
|
||||
"Error"
|
||||
]
|
||||
}
|
||||
],
|
||||
[
|
||||
"transform-regenerator"
|
||||
]
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest --no-cache ./*",
|
||||
"watch": "jest --no-cache --watch ./*",
|
||||
"lint": "eslint .",
|
||||
"lint-test": "eslint . && jest --no-cache ./* "
|
||||
},
|
||||
"eslintConfig": {
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 6,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"env": {
|
||||
"es6": true,
|
||||
"node": true,
|
||||
"jest": true
|
||||
},
|
||||
"extends": "airbnb",
|
||||
"rules": {
|
||||
"import/no-unresolved": "off",
|
||||
"import/extensions": "off"
|
||||
}
|
||||
},
|
||||
"licenses": [
|
||||
"MIT"
|
||||
],
|
||||
"dependencies": {}
|
||||
}
|
31
javascript/run-length-encoding/run-length-encoding.js
Normal file
31
javascript/run-length-encoding/run-length-encoding.js
Normal file
@ -0,0 +1,31 @@
|
||||
exports.encode = function (string) {
|
||||
let counter = 1;
|
||||
let output = '';
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
if (string[i] == string[i + 1]) {
|
||||
counter++;
|
||||
} else {
|
||||
if (counter > 1) {
|
||||
output += counter;
|
||||
}
|
||||
output += string[i];
|
||||
counter = 1;
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
|
||||
exports.decode = function (string) {
|
||||
let output = '';
|
||||
let number = '';
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
if (!isNaN(parseInt(string[i]))) {
|
||||
number += string[i];
|
||||
} else {
|
||||
const count = isNaN(parseInt(number)) ? 1 : parseInt(number);
|
||||
output += string[i].repeat(count);
|
||||
number = '';
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
59
javascript/run-length-encoding/run-length-encoding.spec.js
Normal file
59
javascript/run-length-encoding/run-length-encoding.spec.js
Normal file
@ -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');
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user