javascript - all-your-base

This commit is contained in:
James Walker 2018-09-24 15:11:43 -04:00
parent 0a632f27dd
commit 9304aca5a2
Signed by: walkah
GPG Key ID: 3C127179D6086E93
5 changed files with 7402 additions and 0 deletions

View File

@ -0,0 +1,64 @@
# All Your Base
Convert a number, represented as a sequence of digits in one base, to any other base.
Implement general base conversion. Given a number in base **a**,
represented as a sequence of digits, convert it to base **b**.
## Note
- Try to implement the conversion yourself.
Do not use something else to perform the conversion for you.
## About [Positional Notation](https://en.wikipedia.org/wiki/Positional_notation)
In positional notation, a number in base **b** can be understood as a linear
combination of powers of **b**.
The number 42, *in base 10*, means:
(4 * 10^1) + (2 * 10^0)
The number 101010, *in base 2*, means:
(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)
The number 1120, *in base 3*, means:
(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)
I think you got the idea!
*Yes. Those three numbers above are exactly the same. Congratulations!*
## 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`.
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

View File

@ -0,0 +1,41 @@
class Converter {
convert(digits, inputBase, outputBase) {
// validate in/out bases
if (inputBase < 2 || !Number.isInteger(inputBase)) {
throw Error('Wrong input base')
}
if (outputBase < 2 || !Number.isInteger(outputBase)) {
throw Error('Wrong output base')
}
// Validate digits.
if (!digits.length || (digits[0] == 0 && digits.length != 1)) {
throw Error('Input has wrong format');
} else if (digits[0] == 0 && digits.length == 1){
return digits;
}
// Convert to base 10
let sum = 0;
let i = 0;
while (digits.length > 0) {
let d = digits.pop();
if (d >= inputBase || d < 0) {
throw Error('Input has wrong format');
}
sum += d * (inputBase ** i);
i++;
}
// Convert to outputBase
let output = [];
while (sum > 0) {
output.unshift(sum % outputBase);
sum = Math.floor(sum / outputBase);
}
return output;
}
}
export default Converter;

View File

@ -0,0 +1,137 @@
import Converter from './all-your-base';
const converter = new Converter();
describe('Converter', () => {
test('single bit one to decimal', () => {
expect(converter.convert([1], 2, 10)).toEqual([1]);
});
test('binary to single decimal', () => {
expect(converter.convert([1, 0, 1], 2, 10)).toEqual([5]);
});
test('single decimal to binary', () => {
expect(converter.convert([5], 10, 2)).toEqual([1, 0, 1]);
});
test('binary to multiple decimal', () => {
expect(converter.convert([1, 0, 1, 0, 1, 0], 2, 10)).toEqual([4, 2]);
});
test('decimal to binary', () => {
expect(converter.convert([4, 2], 10, 2)).toEqual([1, 0, 1, 0, 1, 0]);
});
test('trinary to hexadecimal', () => {
expect(converter.convert([1, 1, 2, 0], 3, 16)).toEqual([2, 10]);
});
test('hexadecimal to trinary', () => {
expect(converter.convert([2, 10], 16, 3)).toEqual([1, 1, 2, 0]);
});
test('15-bit integer', () => {
expect(converter.convert([3, 46, 60], 97, 73)).toEqual([6, 10, 45]);
});
test('empty list', () => {
expect(() => {
converter.convert([], 2, 10);
}).toThrow(new Error('Input has wrong format'));
});
test('single zero', () => {
expect(converter.convert([0], 10, 2)).toEqual([0]);
});
test('multiple zeros', () => {
expect(() => {
converter.convert([0, 0, 0], 10, 2);
}).toThrow(new Error('Input has wrong format'));
});
test('leading zeros', () => {
expect(() => {
converter.convert([0, 6, 0], 7, 10);
}).toThrow(new Error('Input has wrong format'));
});
test('negative digit', () => {
expect(() => {
converter.convert([1, -1, 1, 0, 1, 0], 2, 10);
}).toThrow(new Error('Input has wrong format'));
});
test('invalid positive digit', () => {
expect(() => {
converter.convert([1, 2, 1, 0, 1, 0], 2, 10);
}).toThrow(new Error('Input has wrong format'));
});
test('first base is one', () => {
expect(() => {
converter.convert([], 1, 10);
}).toThrow(new Error('Wrong input base'));
});
test('second base is one', () => {
expect(() => {
converter.convert([1, 0, 1, 0, 1, 0], 2, 1);
}).toThrow(new Error('Wrong output base'));
});
test('first base is zero', () => {
expect(() => {
converter.convert([], 0, 10);
}).toThrow(new Error('Wrong input base'));
});
test('second base is zero', () => {
expect(() => {
converter.convert([7], 10, 0);
}).toThrow(new Error('Wrong output base'));
});
test('first base is negative', () => {
expect(() => {
converter.convert([1], -2, 10);
}).toThrow(new Error('Wrong input base'));
});
test('second base is negative', () => {
expect(() => {
converter.convert([1], 2, -7);
}).toThrow(new Error('Wrong output base'));
});
test('both bases are negative', () => {
expect(() => {
converter.convert([1], -2, -7);
}).toThrow(new Error('Wrong input base'));
});
test('missing input base throws an error', () => {
expect(() => {
converter.convert([0]);
}).toThrow(new Error('Wrong input base'));
});
test('wrong input_base base not integer', () => {
expect(() => {
converter.convert([0], 2.5);
}).toThrow(new Error('Wrong input base'));
});
test('missing output base throws an error', () => {
expect(() => {
converter.convert([0], 2);
}).toThrow(new Error('Wrong output base'));
});
test('wrong output_base base not integer', () => {
expect(() => {
converter.convert([0], 3, 2.5);
}).toThrow(new Error('Wrong output base'));
});
});

7080
javascript/all-your-base/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View 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": {}
}