Skip to content

Commit 180ace4

Browse files
authored
Two bucket (#340)
* allow test script to test a single exercise * add two-bucket exercise
1 parent a993cf7 commit 180ace4

File tree

8 files changed

+302
-10
lines changed

8 files changed

+302
-10
lines changed

bin/test-exercises.sh

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1+
#!/usr/bin/env bash
2+
13
test_run() {
24
dic=$(mktemp -d)
5+
trap 'rm -r "$dic"' EXIT
36
echo "Testing $1"
4-
source_file="$(jq -r '.files.solution[0]' $1/.meta/config.json)"
5-
test_file="$(jq -r '.files.test[0]' $1/.meta/config.json)"
7+
source_file="$(jq -r '.files.solution[0]' "$1/.meta/config.json")"
8+
test_file="$(jq -r '.files.test[0]' "$1/.meta/config.json")"
69
echo "source_file: ${source_file}"
710
cat "$1/.meta/example.coffee" > "${dic}/${source_file}"
811
cat "$1/${test_file}" > "${dic}/${test_file}"
9-
sed -i -e 's/xit/it/g' ${dic}/${test_file}
12+
sed -i -e 's/xit/it/g' "${dic}/${test_file}"
1013
jasmine-node --coffee "${dic}/${test_file}" || exit 1
1114
}
1215

13-
for exercise in ./exercises/practice/*; do
14-
test_run ${exercise}
15-
done
16-
17-
if [ -d "./exercises/concept" ]; then
18-
for exercise in ./exercises/concept/*; do
19-
test_run ${exercise}
16+
if [ -n "$1" ] && [ -d "$1" ]; then
17+
# test one exercise
18+
test_run "$1"
19+
else
20+
# test them all
21+
for exercise in ./exercises/practice/*; do
22+
test_run "${exercise}"
2023
done
24+
25+
if [ -d "./exercises/concept" ]; then
26+
for exercise in ./exercises/concept/*; do
27+
test_run "${exercise}"
28+
done
29+
fi
2130
fi

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,14 @@
722722
"practices": [],
723723
"prerequisites": [],
724724
"difficulty": 8
725+
},
726+
{
727+
"slug": "two-bucket",
728+
"name": "Two Bucket",
729+
"uuid": "fa32f8ae-1032-4981-9fa6-5895a549f6a1",
730+
"practices": [],
731+
"prerequisites": [],
732+
"difficulty": 5
725733
}
726734
]
727735
},
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Instructions
2+
3+
Given two buckets of different size and which bucket to fill first, determine how many actions are required to measure an exact number of liters by strategically transferring fluid between the buckets.
4+
5+
There are some rules that your solution must follow:
6+
7+
- You can only do one action at a time.
8+
- There are only 3 possible actions:
9+
1. Pouring one bucket into the other bucket until either:
10+
a) the first bucket is empty
11+
b) the second bucket is full
12+
2. Emptying a bucket and doing nothing to the other.
13+
3. Filling a bucket and doing nothing to the other.
14+
- After an action, you may not arrive at a state where the initial starting bucket is empty and the other bucket is full.
15+
16+
Your program will take as input:
17+
18+
- the size of bucket one
19+
- the size of bucket two
20+
- the desired number of liters to reach
21+
- which bucket to fill first, either bucket one or bucket two
22+
23+
Your program should determine:
24+
25+
- the total number of actions it should take to reach the desired number of liters, including the first fill of the starting bucket
26+
- which bucket should end up with the desired number of liters - either bucket one or bucket two
27+
- how many liters are left in the other bucket
28+
29+
Note: any time a change is made to either or both buckets counts as one (1) action.
30+
31+
Example:
32+
Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters.
33+
Let's say at a given step, bucket one is holding 7 liters and bucket two is holding 8 liters (7,8).
34+
If you empty bucket one and make no change to bucket two, leaving you with 0 liters and 8 liters respectively (0,8), that counts as one action.
35+
Instead, if you had poured from bucket one into bucket two until bucket two was full, resulting in 4 liters in bucket one and 11 liters in bucket two (4,11), that would also only count as one action.
36+
37+
Another Example:
38+
Bucket one can hold 3 liters, and bucket two can hold up to 5 liters.
39+
You are told you must start with bucket one.
40+
So your first action is to fill bucket one.
41+
You choose to empty bucket one for your second action.
42+
For your third action, you may not fill bucket two, because this violates the third rule -- you may not end up in a state after any action where the starting bucket is empty and the other bucket is full.
43+
44+
Written with <3 at [Fullstack Academy][fullstack] by Lindsay Levine.
45+
46+
[fullstack]: https://www.fullstackacademy.com/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"two-bucket.coffee"
8+
],
9+
"test": [
10+
"two-bucket.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.",
17+
"source": "Water Pouring Problem",
18+
"source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/"
19+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Bucket
2+
constructor: (@name, @size) ->
3+
@amount = 0
4+
5+
isFull: -> @amount is @size
6+
isEmpty: -> @amount is 0
7+
available: -> @size - @amount
8+
9+
fill: -> @amount = @size
10+
empty: -> @amount = 0
11+
12+
pourInto: (other) ->
13+
quantity = Math.min @amount, other.available()
14+
@amount -= quantity
15+
other.amount += quantity
16+
17+
18+
gcd = (a, b) -> if b == 0 then a else gcd(b, a % b)
19+
20+
21+
class TwoBucket
22+
constructor: ({bucketOne, bucketTwo, @goal, startBucket}) ->
23+
# validation
24+
throw new Error 'impossible' if @goal > Math.max(bucketOne, bucketTwo)
25+
g = gcd(bucketOne, bucketTwo)
26+
throw new Error 'impossible' if @goal % g isnt 0
27+
28+
@first = new Bucket("one", bucketOne)
29+
@second = new Bucket("two", bucketTwo)
30+
if startBucket == "two"
31+
[@first, @second] = [@second, @first]
32+
33+
measure: ->
34+
moves = 0
35+
36+
@first.fill()
37+
moves++
38+
39+
if @second.size == @goal
40+
@second.fill()
41+
moves++
42+
43+
loop
44+
return @result(@first, @second, moves) if @first.amount == @goal
45+
return @result(@second, @first, moves) if @second.amount == @goal
46+
47+
if @first.isEmpty()
48+
@first.fill()
49+
else if @second.isFull()
50+
@second.empty()
51+
else
52+
@first.pourInto @second
53+
54+
moves++
55+
56+
result: (winner, loser, moves) ->
57+
{ moves: moves, goalBucket: winner.name, otherBucket: loser.amount }
58+
59+
60+
module.exports = TwoBucket
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.
11+
12+
[a6f2b4ba-065f-4dca-b6f0-e3eee51cb661]
13+
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"
14+
15+
[6c4ea451-9678-4926-b9b3-68364e066d40]
16+
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two"
17+
18+
[3389f45e-6a56-46d5-9607-75aa930502ff]
19+
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one"
20+
21+
[fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1]
22+
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two"
23+
24+
[0ee1f57e-da84-44f7-ac91-38b878691602]
25+
description = "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two"
26+
27+
[eb329c63-5540-4735-b30b-97f7f4df0f84]
28+
description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"
29+
30+
[449be72d-b10a-4f4b-a959-ca741e333b72]
31+
description = "Not possible to reach the goal"
32+
33+
[aac38b7a-77f4-4d62-9b91-8846d533b054]
34+
description = "With the same buckets but a different goal, then it is possible"
35+
36+
[74633132-0ccf-49de-8450-af4ab2e3b299]
37+
description = "Goal larger than both buckets is impossible"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class TwoBucket
2+
constructor: ({bucketOne, bucketTwo, goal, startBucket}) ->
3+
4+
measure: ->
5+
6+
module.exports = TwoBucket
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
TwoBucket = require './two-bucket'
2+
3+
describe 'TwoBucket', ->
4+
5+
it "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one", ->
6+
twoBucket = new TwoBucket({
7+
bucketOne: 3
8+
bucketTwo: 5
9+
goal: 1
10+
startBucket: "one"
11+
})
12+
result = twoBucket.measure()
13+
expect(result.moves).toEqual(4)
14+
expect(result.goalBucket).toEqual("one")
15+
expect(result.otherBucket).toEqual(5)
16+
17+
xit "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two", ->
18+
twoBucket = new TwoBucket({
19+
bucketOne: 3
20+
bucketTwo: 5
21+
goal: 1
22+
startBucket: "two"
23+
})
24+
result = twoBucket.measure()
25+
expect(result.moves).toEqual(8)
26+
expect(result.goalBucket).toEqual("two")
27+
expect(result.otherBucket).toEqual(3)
28+
29+
xit "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one", ->
30+
twoBucket = new TwoBucket({
31+
bucketOne: 7
32+
bucketTwo: 11
33+
goal: 2
34+
startBucket: "one"
35+
})
36+
result = twoBucket.measure()
37+
expect(result.moves).toEqual(14)
38+
expect(result.goalBucket).toEqual("one")
39+
expect(result.otherBucket).toEqual(11)
40+
41+
xit "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two", ->
42+
twoBucket = new TwoBucket({
43+
bucketOne: 7
44+
bucketTwo: 11
45+
goal: 2
46+
startBucket: "two"
47+
})
48+
result = twoBucket.measure()
49+
expect(result.moves).toEqual(18)
50+
expect(result.goalBucket).toEqual("two")
51+
expect(result.otherBucket).toEqual(7)
52+
53+
xit "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two", ->
54+
twoBucket = new TwoBucket({
55+
bucketOne: 1
56+
bucketTwo: 3
57+
goal: 3
58+
startBucket: "two"
59+
})
60+
result = twoBucket.measure()
61+
expect(result.moves).toEqual(1)
62+
expect(result.goalBucket).toEqual("two")
63+
expect(result.otherBucket).toEqual(0)
64+
65+
xit "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two", ->
66+
twoBucket = new TwoBucket({
67+
bucketOne: 2
68+
bucketTwo: 3
69+
goal: 3
70+
startBucket: "one"
71+
})
72+
result = twoBucket.measure()
73+
expect(result.moves).toEqual(2)
74+
expect(result.goalBucket).toEqual("two")
75+
expect(result.otherBucket).toEqual(2)
76+
77+
xit "Not possible to reach the goal", ->
78+
input = {
79+
bucketOne: 6
80+
bucketTwo: 15
81+
goal: 5
82+
startBucket: "one"
83+
}
84+
expect(-> new TwoBucket(input)).toThrow("impossible")
85+
86+
xit "With the same buckets but a different goal, then it is possible", ->
87+
twoBucket = new TwoBucket({
88+
bucketOne: 6
89+
bucketTwo: 15
90+
goal: 9
91+
startBucket: "one"
92+
})
93+
result = twoBucket.measure()
94+
expect(result.moves).toEqual(10)
95+
expect(result.goalBucket).toEqual("two")
96+
expect(result.otherBucket).toEqual(0)
97+
98+
xit "Goal larger than both buckets is impossible", ->
99+
input = {
100+
bucketOne: 5
101+
bucketTwo: 7
102+
goal: 8
103+
startBucket: "one"
104+
}
105+
expect ->
106+
new TwoBucket(input)
107+
.toThrow("impossible")

0 commit comments

Comments
 (0)