Skip to content

Commit

Permalink
Fixed two-bucket solution & errant tests (#905) (#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
qianzhong516 authored Oct 3, 2021
1 parent 15e899f commit f1960a2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
20 changes: 19 additions & 1 deletion exercises/practice/two-bucket/.meta/proof.ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,29 @@ export class TwoBucket {
return mvCount;
}

gcd(a, b) {
// greatest common divisor
if (!b) {
return a;
}
return this.gcd(b, a % b);
}

moves() {
let j = 0;
// j will be running val of bucket one, k = running val of bucket two
let j = 0;
let k = 0;

// if the goal is not a multiple of the gcd of bucket one and bucket two,
// or the goal is bigger than both buckets,
// the solution will be impossible.
if (
this.z % this.gcd(this.x, this.y) !== 0 ||
(this.z > this.x && this.z > this.y)
) {
throw new Error('Cannot reach the goal.');
}

if (this.starter === 'one') {
j = this.x;
} else {
Expand Down
13 changes: 10 additions & 3 deletions exercises/practice/two-bucket/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[a6f2b4ba-065f-4dca-b6f0-e3eee51cb661]
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"
Expand Down
20 changes: 14 additions & 6 deletions exercises/practice/two-bucket/two-bucket.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,34 @@ describe('TwoBucket', () => {
});

describe('Measure using bucket one of size 2 and bucket two of size 3', () => {
test.skip('start with bucket one and end with bucket two', () => {
xtest('start with bucket one and end with bucket two', () => {
const twoBucket = new TwoBucket(2, 3, 3, 'one');
expect(twoBucket.moves()).toEqual(2);
expect(twoBucket.moves()).toEqual(4);
expect(twoBucket.goalBucket).toEqual('two');
expect(twoBucket.otherBucket).toEqual(2);
expect(twoBucket.otherBucket).toEqual(1);
});
});

describe('Reachability', () => {
const buckOne = 6;
const buckTwo = 15;
const starterBuck = 'one';

test.skip('Not possible to reach the goal', () => {
xtest('Not possible to reach the goal, start with bucket one', () => {
const starterBuck = 'one';
const goal = 5;
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck);
expect(() => twoBucket.moves()).toThrow();
});

xtest('Not possible to reach the goal, start with bucket two', () => {
const starterBuck = 'two';
const goal = 5;
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck);
expect(() => twoBucket.moves()).toThrow();
});

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

describe('Goal larger than both buckets', () => {
test.skip('Is impossible', () => {
xtest('Is impossible', () => {
const twoBucket = new TwoBucket(5, 7, 8, 'one');
expect(() => twoBucket.moves()).toThrow();
});
Expand Down

0 comments on commit f1960a2

Please sign in to comment.