Skip to content

Commit f1960a2

Browse files
authored
Fixed two-bucket solution & errant tests (#905) (#1396)
1 parent 15e899f commit f1960a2

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

exercises/practice/two-bucket/.meta/proof.ci.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,29 @@ export class TwoBucket {
9595
return mvCount;
9696
}
9797

98+
gcd(a, b) {
99+
// greatest common divisor
100+
if (!b) {
101+
return a;
102+
}
103+
return this.gcd(b, a % b);
104+
}
105+
98106
moves() {
99-
let j = 0;
100107
// j will be running val of bucket one, k = running val of bucket two
108+
let j = 0;
101109
let k = 0;
102110

111+
// if the goal is not a multiple of the gcd of bucket one and bucket two,
112+
// or the goal is bigger than both buckets,
113+
// the solution will be impossible.
114+
if (
115+
this.z % this.gcd(this.x, this.y) !== 0 ||
116+
(this.z > this.x && this.z > this.y)
117+
) {
118+
throw new Error('Cannot reach the goal.');
119+
}
120+
103121
if (this.starter === 'one') {
104122
j = this.x;
105123
} else {

exercises/practice/two-bucket/.meta/tests.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[a6f2b4ba-065f-4dca-b6f0-e3eee51cb661]
613
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"

exercises/practice/two-bucket/two-bucket.spec.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,34 @@ describe('TwoBucket', () => {
5959
});
6060

6161
describe('Measure using bucket one of size 2 and bucket two of size 3', () => {
62-
test.skip('start with bucket one and end with bucket two', () => {
62+
xtest('start with bucket one and end with bucket two', () => {
6363
const twoBucket = new TwoBucket(2, 3, 3, 'one');
64-
expect(twoBucket.moves()).toEqual(2);
64+
expect(twoBucket.moves()).toEqual(4);
6565
expect(twoBucket.goalBucket).toEqual('two');
66-
expect(twoBucket.otherBucket).toEqual(2);
66+
expect(twoBucket.otherBucket).toEqual(1);
6767
});
6868
});
6969

7070
describe('Reachability', () => {
7171
const buckOne = 6;
7272
const buckTwo = 15;
73-
const starterBuck = 'one';
7473

75-
test.skip('Not possible to reach the goal', () => {
74+
xtest('Not possible to reach the goal, start with bucket one', () => {
75+
const starterBuck = 'one';
76+
const goal = 5;
77+
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck);
78+
expect(() => twoBucket.moves()).toThrow();
79+
});
80+
81+
xtest('Not possible to reach the goal, start with bucket two', () => {
82+
const starterBuck = 'two';
7683
const goal = 5;
7784
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck);
7885
expect(() => twoBucket.moves()).toThrow();
7986
});
8087

8188
xtest('With the same buckets but a different goal, then it is possible', () => {
89+
const starterBuck = 'one';
8290
const goal = 9;
8391
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck);
8492
expect(twoBucket.moves()).toEqual(10);
@@ -88,7 +96,7 @@ describe('TwoBucket', () => {
8896
});
8997

9098
describe('Goal larger than both buckets', () => {
91-
test.skip('Is impossible', () => {
99+
xtest('Is impossible', () => {
92100
const twoBucket = new TwoBucket(5, 7, 8, 'one');
93101
expect(() => twoBucket.moves()).toThrow();
94102
});

0 commit comments

Comments
 (0)