Skip to content

Commit 6d2a48f

Browse files
committed
Adding tests for "Continued fractions".
1 parent bb15069 commit 6d2a48f

File tree

16 files changed

+190
-0
lines changed

16 files changed

+190
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Tests for "Langton's ant
2828
- Tests for "Anagrams".
2929
- Tests for "The stonemason".
30+
- Tests for "Continued fractions".
3031

3132
## [1.14.0] - 2022-10-31
3233
### Added
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* The "Continued fractions" puzzle.
3+
* @see {@link https://www.codingame.com/ide/puzzle/continued-fractions}
4+
*/
5+
function execute(readline) {
6+
const fraction = readline();
7+
8+
// Write an answer using console.log()
9+
// To debug: console.error('Debug messages...');
10+
11+
console.log('p/q or [x0; x1, ..., xn]');
12+
}
13+
14+
export { execute };
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { assert } from 'chai';
2+
import sinon from 'sinon';
3+
import File from '../../../../File.js';
4+
import { execute } from '../../../../../lib/community/training/medium/continuedFractions/continuedFractions.js';
5+
6+
const __dirname = new URL('.', import.meta.url).pathname;
7+
8+
suite("Continued fractions", function() {
9+
const sandbox = sinon.createSandbox();
10+
11+
setup(function () {
12+
sandbox.stub(console, "log");
13+
});
14+
15+
teardown(function () {
16+
sandbox.restore();
17+
});
18+
19+
20+
test("F -> CF", function() {
21+
let inputFile = new File(__dirname + 'input/01 - F -> CF.txt');
22+
23+
execute(inputFile.readline.bind(inputFile));
24+
25+
assert.strictEqual(
26+
console.log.getCall(0).args[0],
27+
"[1; 2, 3]"
28+
);
29+
});
30+
31+
test("CF -> F", function() {
32+
let inputFile = new File(__dirname + 'input/02 - CF -> F.txt');
33+
34+
execute(inputFile.readline.bind(inputFile));
35+
36+
assert.strictEqual(
37+
console.log.getCall(0).args[0],
38+
"71/31"
39+
);
40+
});
41+
42+
test("Small fraction", function() {
43+
let inputFile = new File(__dirname + 'input/03 - small fraction.txt');
44+
45+
execute(inputFile.readline.bind(inputFile));
46+
47+
assert.strictEqual(
48+
console.log.getCall(0).args[0],
49+
"[0; 1, 2, 2]"
50+
);
51+
});
52+
53+
test("Small CF", function() {
54+
let inputFile = new File(__dirname + 'input/04 - small CF.txt');
55+
56+
execute(inputFile.readline.bind(inputFile));
57+
58+
assert.strictEqual(
59+
console.log.getCall(0).args[0],
60+
"19/24"
61+
);
62+
});
63+
64+
test("Medium fraction", function() {
65+
let inputFile = new File(__dirname + 'input/05 - medium fraction.txt');
66+
67+
execute(inputFile.readline.bind(inputFile));
68+
69+
assert.strictEqual(
70+
console.log.getCall(0).args[0],
71+
"[7; 5, 1, 3, 1, 1, 2]"
72+
);
73+
});
74+
75+
test("Medium CF", function() {
76+
let inputFile = new File(__dirname + 'input/06 - medium CF.txt');
77+
78+
execute(inputFile.readline.bind(inputFile));
79+
80+
assert.strictEqual(
81+
console.log.getCall(0).args[0],
82+
"13887/1117"
83+
);
84+
});
85+
86+
test("Large fraction", function() {
87+
let inputFile = new File(__dirname + 'input/07 - large fraction.txt');
88+
89+
execute(inputFile.readline.bind(inputFile));
90+
91+
assert.strictEqual(
92+
console.log.getCall(0).args[0],
93+
"[6; 4, 20, 1, 25, 6]"
94+
);
95+
});
96+
97+
test("Large CF", function() {
98+
let inputFile = new File(__dirname + 'input/08 - large CF.txt');
99+
100+
execute(inputFile.readline.bind(inputFile));
101+
102+
assert.strictEqual(
103+
console.log.getCall(0).args[0],
104+
"416779/119113"
105+
);
106+
});
107+
108+
test("F -> Long CF", function() {
109+
let inputFile = new File(__dirname + 'input/09 - F -> long CF.txt');
110+
111+
execute(inputFile.readline.bind(inputFile));
112+
113+
assert.strictEqual(
114+
console.log.getCall(0).args[0],
115+
"[1; 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 1, 1, 1, 2, 1, 2]"
116+
);
117+
});
118+
119+
test("Long CF -> F", function() {
120+
let inputFile = new File(__dirname + 'input/10 - Long CF -> F.txt');
121+
122+
execute(inputFile.readline.bind(inputFile));
123+
124+
assert.strictEqual(
125+
console.log.getCall(0).args[0],
126+
"500287/154989"
127+
);
128+
});
129+
130+
test("Negative fraction", function() {
131+
let inputFile = new File(__dirname + 'input/11 - negative fraction.txt');
132+
133+
execute(inputFile.readline.bind(inputFile));
134+
135+
assert.strictEqual(
136+
console.log.getCall(0).args[0],
137+
"[-2; 1, 11]"
138+
);
139+
});
140+
141+
test("Negative CF", function() {
142+
let inputFile = new File(__dirname + 'input/12 - negative CF.txt');
143+
144+
execute(inputFile.readline.bind(inputFile));
145+
146+
assert.strictEqual(
147+
console.log.getCall(0).args[0],
148+
"-833719/265381"
149+
);
150+
});
151+
152+
test("All together now", function() {
153+
let inputFile = new File(__dirname + 'input/13 - all together now.txt');
154+
155+
execute(inputFile.readline.bind(inputFile));
156+
157+
assert.strictEqual(
158+
console.log.getCall(0).args[0],
159+
"-833719/265381"
160+
);
161+
});
162+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10/7
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[2; 3, 2, 4]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5/7
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[0; 1, 3, 1, 4]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
954/133
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[12; 2, 3, 5, 30]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
83217/13321

0 commit comments

Comments
 (0)