Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f43df0a

Browse files
author
programmiri
committed
Add exercise All Your Base
1 parent 835563f commit f43df0a

File tree

6 files changed

+5075
-0
lines changed

6 files changed

+5075
-0
lines changed

‎all-your-base/.eslintrc‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"env": {
9+
"es6": true,
10+
"node": true,
11+
"jest": true
12+
},
13+
"extends": [
14+
"eslint:recommended",
15+
"plugin:import/errors",
16+
"plugin:import/warnings"
17+
],
18+
"rules": {
19+
"linebreak-style": "off",
20+
21+
"import/extensions": "off",
22+
"import/no-default-export": "off",
23+
"import/no-unresolved": "off",
24+
"import/prefer-default-export": "off"
25+
}
26+
}

‎all-your-base/README.md‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# All Your Base
2+
3+
Convert a number, represented as a sequence of digits in one base, to any other base.
4+
5+
Implement general base conversion. Given a number in base **a**,
6+
represented as a sequence of digits, convert it to base **b**.
7+
8+
## Note
9+
10+
- Try to implement the conversion yourself.
11+
Do not use something else to perform the conversion for you.
12+
13+
## About [Positional Notation](https://en.wikipedia.org/wiki/Positional_notation)
14+
15+
In positional notation, a number in base **b** can be understood as a linear
16+
combination of powers of **b**.
17+
18+
The number 42, *in base 10*, means:
19+
20+
(4 * 10^1) + (2 * 10^0)
21+
22+
The number 101010, *in base 2*, means:
23+
24+
(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)
25+
26+
The number 1120, *in base 3*, means:
27+
28+
(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)
29+
30+
I think you got the idea!
31+
32+
*Yes. Those three numbers above are exactly the same. Congratulations!*
33+
34+
## Setup
35+
36+
Go through the setup instructions for Javascript to install the necessary
37+
dependencies:
38+
39+
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
40+
41+
## Requirements
42+
43+
Install assignment dependencies:
44+
45+
```bash
46+
$ npm install
47+
```
48+
49+
## Making the test suite pass
50+
51+
Execute the tests with:
52+
53+
```bash
54+
$ npm test
55+
```
56+
57+
In the test suites all tests but the first have been skipped.
58+
59+
Once you get a test passing, you can enable the next one by changing `xtest` to
60+
`test`.
61+
62+
63+
## Submitting Incomplete Solutions
64+
65+
It's possible to submit an incomplete solution so you can see how others have
66+
completed the exercise.

‎all-your-base/all-your-base.spec.js‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { convert } from './all-your-base';
2+
3+
describe('Converter', () => {
4+
test('single bit one to decimal', () => {
5+
expect(convert([1], 2, 10)).toEqual([1]);
6+
});
7+
8+
xtest('binary to single decimal', () => {
9+
expect(convert([1, 0, 1], 2, 10)).toEqual([5]);
10+
});
11+
12+
xtest('single decimal to binary', () => {
13+
expect(convert([5], 10, 2)).toEqual([1, 0, 1]);
14+
});
15+
16+
xtest('binary to multiple decimal', () => {
17+
expect(convert([1, 0, 1, 0, 1, 0], 2, 10)).toEqual([4, 2]);
18+
});
19+
20+
xtest('decimal to binary', () => {
21+
expect(convert([4, 2], 10, 2)).toEqual([1, 0, 1, 0, 1, 0]);
22+
});
23+
24+
xtest('trinary to hexadecimal', () => {
25+
expect(convert([1, 1, 2, 0], 3, 16)).toEqual([2, 10]);
26+
});
27+
28+
xtest('hexadecimal to trinary', () => {
29+
expect(convert([2, 10], 16, 3)).toEqual([1, 1, 2, 0]);
30+
});
31+
32+
xtest('15-bit integer', () => {
33+
expect(convert([3, 46, 60], 97, 73)).toEqual([6, 10, 45]);
34+
});
35+
36+
xtest('empty list', () => {
37+
expect(() => {
38+
convert([], 2, 10);
39+
}).toThrow(new Error('Input has wrong format'));
40+
});
41+
42+
xtest('single zero', () => {
43+
expect(convert([0], 10, 2)).toEqual([0]);
44+
});
45+
46+
xtest('multiple zeros', () => {
47+
expect(() => {
48+
convert([0, 0, 0], 10, 2);
49+
}).toThrow(new Error('Input has wrong format'));
50+
});
51+
52+
xtest('leading zeros', () => {
53+
expect(() => {
54+
convert([0, 6, 0], 7, 10);
55+
}).toThrow(new Error('Input has wrong format'));
56+
});
57+
58+
xtest('negative digit', () => {
59+
expect(() => {
60+
convert([1, -1, 1, 0, 1, 0], 2, 10);
61+
}).toThrow(new Error('Input has wrong format'));
62+
});
63+
64+
xtest('invalid positive digit', () => {
65+
expect(() => {
66+
convert([1, 2, 1, 0, 1, 0], 2, 10);
67+
}).toThrow(new Error('Input has wrong format'));
68+
});
69+
70+
xtest('first base is one', () => {
71+
expect(() => {
72+
convert([], 1, 10);
73+
}).toThrow(new Error('Wrong input base'));
74+
});
75+
76+
xtest('second base is one', () => {
77+
expect(() => {
78+
convert([1, 0, 1, 0, 1, 0], 2, 1);
79+
}).toThrow(new Error('Wrong output base'));
80+
});
81+
82+
xtest('first base is zero', () => {
83+
expect(() => {
84+
convert([], 0, 10);
85+
}).toThrow(new Error('Wrong input base'));
86+
});
87+
88+
xtest('second base is zero', () => {
89+
expect(() => {
90+
convert([7], 10, 0);
91+
}).toThrow(new Error('Wrong output base'));
92+
});
93+
94+
xtest('first base is negative', () => {
95+
expect(() => {
96+
convert([1], -2, 10);
97+
}).toThrow(new Error('Wrong input base'));
98+
});
99+
100+
xtest('second base is negative', () => {
101+
expect(() => {
102+
convert([1], 2, -7);
103+
}).toThrow(new Error('Wrong output base'));
104+
});
105+
106+
xtest('both bases are negative', () => {
107+
expect(() => {
108+
convert([1], -2, -7);
109+
}).toThrow(new Error('Wrong input base'));
110+
});
111+
112+
xtest('missing input base throws an error', () => {
113+
expect(() => {
114+
convert([0]);
115+
}).toThrow(new Error('Wrong input base'));
116+
});
117+
118+
xtest('wrong input_base base not integer', () => {
119+
expect(() => {
120+
convert([0], 2.5);
121+
}).toThrow(new Error('Wrong input base'));
122+
});
123+
124+
xtest('missing output base throws an error', () => {
125+
expect(() => {
126+
convert([0], 2);
127+
}).toThrow(new Error('Wrong output base'));
128+
});
129+
130+
xtest('wrong output_base base not integer', () => {
131+
expect(() => {
132+
convert([0], 3, 2.5);
133+
}).toThrow(new Error('Wrong output base'));
134+
});
135+
});

‎all-your-base/babel.config.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
useBuiltIns: false,
10+
},
11+
12+
],
13+
],
14+
};

‎all-your-base/package.json‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "exercism-javascript",
3+
"version": "0.0.0",
4+
"description": "Exercism exercises in Javascript.",
5+
"author": "Katrina Owen",
6+
"private": true,
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/exercism/javascript"
10+
},
11+
"devDependencies": {
12+
"@babel/cli": "^7.2.3",
13+
"@babel/core": "^7.4.0",
14+
"@babel/preset-env": "^7.4.2",
15+
"babel-eslint": "^10.0.1",
16+
"babel-jest": "^24.5.0",
17+
"eslint": "^5.15.3",
18+
"eslint-plugin-import": "^2.16.0",
19+
"jest": "^24.5.0"
20+
},
21+
"jest": {
22+
"modulePathIgnorePatterns": [
23+
"package.json"
24+
]
25+
},
26+
"scripts": {
27+
"test": "jest --no-cache ./*",
28+
"watch": "jest --no-cache --watch ./*",
29+
"lint": "eslint .",
30+
"lint-test": "eslint . && jest --no-cache ./* "
31+
},
32+
"license": "MIT",
33+
"dependencies": {}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /