From c2b862d6b45d2143bd59285673bef58c45af4006 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 10 Dec 2024 18:57:53 +0530 Subject: [PATCH 01/10] Syncing test.tolm for two-bucket --- .../practice/two-bucket/.meta/tests.toml | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/exercises/practice/two-bucket/.meta/tests.toml b/exercises/practice/two-bucket/.meta/tests.toml index 27adad905..d6ff02f53 100644 --- a/exercises/practice/two-bucket/.meta/tests.toml +++ b/exercises/practice/two-bucket/.meta/tests.toml @@ -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" @@ -19,3 +26,12 @@ description = "Measure one step using bucket one of size 1 and bucket two of siz [eb329c63-5540-4735-b30b-97f7f4df0f84] description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two" + +[449be72d-b10a-4f4b-a959-ca741e333b72] +description = "Not possible to reach the goal" + +[aac38b7a-77f4-4d62-9b91-8846d533b054] +description = "With the same buckets but a different goal, then it is possible" + +[74633132-0ccf-49de-8450-af4ab2e3b299] +description = "Goal larger than both buckets is impossible" From 16b0389d6dc1bdd2140dd1eb7e1c04b54f296811 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 10 Dec 2024 19:22:50 +0530 Subject: [PATCH 02/10] Updating test file to match with synced test.toml --- .../practice/two-bucket/.meta/config.json | 1 + .../src/test/java/TwoBucketTest.java | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/exercises/practice/two-bucket/.meta/config.json b/exercises/practice/two-bucket/.meta/config.json index 54da6bd62..4a36413e0 100644 --- a/exercises/practice/two-bucket/.meta/config.json +++ b/exercises/practice/two-bucket/.meta/config.json @@ -4,6 +4,7 @@ ], "contributors": [ "FridaTveit", + "jagdish-15", "jmrunkle", "lemoncurry", "mirkoperillo", diff --git a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java index cf05b67d1..54c4536e7 100644 --- a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java +++ b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java @@ -3,6 +3,8 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.Collections; + public class TwoBucketTest { @Test @@ -75,4 +77,52 @@ public void testBucketOneSizeTwoBucketTwoSizeThreeStartWithOne() { assertThat(twoBucket.getOtherBucket()).isEqualTo(2); } + + @Disabled("Remove to run test") + @Test + public void testReachingGoalIsImpossible() { + + TwoBucket twoBucket = new TwoBucket(6, 15, 5, "one"); + + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> twoBucket.getTotalMoves())) + .withMessage("impossible"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> twoBucket.getFinalBucket())) + .withMessage("impossible"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> twoBucket.getOtherBucket())) + .withMessage("impossible"); + + } + + @Disabled("Remove to run test") + @Test + public void testBucketOneSizeSixBucketTwoSizeFifteenStartWithOne() { + + TwoBucket twoBucket = new TwoBucket(6, 15, 9, "one"); + + assertThat(twoBucket.getTotalMoves()).isEqualTo(10); + assertThat(twoBucket.getFinalBucket()).isEqualTo("two"); + assertThat(twoBucket.getOtherBucket()).isEqualTo(0); + + } + + @Disabled("Remove to run test") + @Test + public void testGoalLargerThanBothBucketsIsImpossible() { + + TwoBucket twoBucket = new TwoBucket(5, 7, 8, "one"); + + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> twoBucket.getTotalMoves())) + .withMessage("impossible"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> twoBucket.getFinalBucket())) + .withMessage("impossible"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> twoBucket.getOtherBucket())) + .withMessage("impossible"); + + } } From 45e971dab1586e02f2783f4d27dc7a9205a819b7 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 10 Dec 2024 19:41:54 +0530 Subject: [PATCH 03/10] Adding missing import statement in test file for two-bucket --- exercises/practice/two-bucket/src/test/java/TwoBucketTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java index 54c4536e7..b9130893d 100644 --- a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java +++ b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import java.util.Collections; From ca31ed314799782c763a81f97a68f789a5572edb Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 26 Dec 2024 17:39:54 +0530 Subject: [PATCH 04/10] Fixing TwoBucketTest.java --- .../two-bucket/src/test/java/TwoBucketTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java index b9130893d..cd0b25612 100644 --- a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java +++ b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java @@ -86,13 +86,13 @@ public void testReachingGoalIsImpossible() { TwoBucket twoBucket = new TwoBucket(6, 15, 5, "one"); assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getTotalMoves())) + .isThrownBy(() -> twoBucket.getTotalMoves()) .withMessage("impossible"); assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getFinalBucket())) + .isThrownBy(() -> twoBucket.getFinalBucket()) .withMessage("impossible"); assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getOtherBucket())) + .isThrownBy(() -> twoBucket.getOtherBucket()) .withMessage("impossible"); } @@ -116,13 +116,13 @@ public void testGoalLargerThanBothBucketsIsImpossible() { TwoBucket twoBucket = new TwoBucket(5, 7, 8, "one"); assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getTotalMoves())) + .isThrownBy(() -> twoBucket.getTotalMoves()) .withMessage("impossible"); assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getFinalBucket())) + .isThrownBy(() -> twoBucket.getFinalBucket()) .withMessage("impossible"); assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getOtherBucket())) + .isThrownBy(() -> twoBucket.getOtherBucket()) .withMessage("impossible"); } From c1e8519744b00095d60aa5c0cd2c6c3fffce23ae Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 26 Dec 2024 19:10:28 +0530 Subject: [PATCH 05/10] Updating the reference answer --- .../practice/two-bucket/.meta/config.json | 1 + .../.meta/src/reference/java/TwoBucket.java | 223 +++++++++--------- .../src/test/java/TwoBucketTest.java | 2 - 3 files changed, 108 insertions(+), 118 deletions(-) diff --git a/exercises/practice/two-bucket/.meta/config.json b/exercises/practice/two-bucket/.meta/config.json index 4a36413e0..65fb5a153 100644 --- a/exercises/practice/two-bucket/.meta/config.json +++ b/exercises/practice/two-bucket/.meta/config.json @@ -7,6 +7,7 @@ "jagdish-15", "jmrunkle", "lemoncurry", + "LoadingBG", "mirkoperillo", "msomji", "muzimuzhi", diff --git a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java index 89771ab81..a60431e2f 100644 --- a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java +++ b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java @@ -1,149 +1,140 @@ import java.util.ArrayList; -import java.util.Objects; +import java.util.List; +import java.util.Map; class TwoBucket { - - private class State { - int moves; - int bucketOne; - int bucketTwo; - - State (int moves, int bucketOne, int bucketTwo) { - this.moves = moves; - this.bucketOne = bucketOne; - this.bucketTwo = bucketTwo; - } - - @Override - public boolean equals(Object o) { - State otherState = (State) o; - return this.moves == otherState.moves && - this.bucketOne == otherState.bucketOne && - this.bucketTwo == otherState.bucketTwo; - } - - @Override - public int hashCode() { - return Objects.hash(moves, bucketOne, bucketTwo); - } - } - - private State finalState; - - private int bucketOneCap; - private int bucketTwoCap; - private int desiredLiters; - private String startBucket; - + private int totalMoves = Integer.MAX_VALUE; + private String finalBucket = ""; + private int otherBucket = Integer.MAX_VALUE; + + private final int bucketOneCap; + private final int bucketTwoCap; + private final int desiredLiters; + TwoBucket(int bucketOneCap, int bucketTwoCap, int desiredLiters, String startBucket) { + fillBuckets( + startBucket.equals("one") ? bucketOneCap : 0, + bucketOneCap, + startBucket.equals("two") ? bucketTwoCap : 0, + bucketTwoCap, + desiredLiters, + 1, + startBucket + ); + this.bucketOneCap = bucketOneCap; this.bucketTwoCap = bucketTwoCap; this.desiredLiters = desiredLiters; - this.startBucket = startBucket; - - finalState = computeFinalState(); } - private ArrayList getAdjacentStates (State state) { - ArrayList adjacentStates = new ArrayList(); - - //Empty bucket one - adjacentStates.add(new State(state.moves + 1, 0, state.bucketTwo)); - - //Empty bucket two - adjacentStates.add(new State(state.moves + 1, state.bucketOne, 0)); - - //Fill bucket one - adjacentStates.add(new State(state.moves + 1, bucketOneCap, state.bucketTwo)); + private List> statesReached = new ArrayList<>(); + + private void fillBuckets( + int bucketOne, + int bucketOneCap, + int bucketTwo, + int bucketTwoCap, + int desiredLiters, + int actionsTaken, + String startingBucket + ) { + if (startingBucket.equals("one") && bucketOne == 0 && bucketTwo == bucketTwoCap + || startingBucket.equals("two") && bucketTwo == 0 && bucketOne == bucketOneCap + || statesReached.contains(Map.entry(bucketOne, bucketTwo)) + || bucketOne > bucketOneCap + || bucketTwo > bucketTwoCap) { + return; + } + + if (bucketOne == desiredLiters) { + if (actionsTaken < totalMoves) { + this.totalMoves = actionsTaken; + this.finalBucket = "one"; + this.otherBucket = bucketTwo; + } + return; + } + if (bucketTwo == desiredLiters) { + if (actionsTaken < totalMoves) { + this.totalMoves = actionsTaken; + this.finalBucket = "two"; + this.otherBucket = bucketOne; + } + return; + } - //Fill bucket two - adjacentStates.add(new State(state.moves + 1, state.bucketOne, bucketTwoCap)); + statesReached.add(Map.entry(bucketOne, bucketTwo)); - //pour from bucket one to bucket two - if (state.bucketOne + state.bucketTwo > bucketTwoCap) { - adjacentStates.add(new State(state.moves + 1, - state.bucketOne - (bucketTwoCap - state.bucketTwo), - bucketTwoCap)); - } else { - adjacentStates.add(new State(state.moves + 1, 0, state.bucketOne + state.bucketTwo)); + if (bucketOne != 0) { + fillBuckets(0, bucketOneCap, bucketTwo, bucketTwoCap, desiredLiters, actionsTaken + 1, startingBucket); } - - //pour from bucket two to bucket one - if (state.bucketTwo + state.bucketOne > bucketOneCap) { - adjacentStates.add(new State(state.moves + 1, - bucketOneCap, - state.bucketTwo - (bucketOneCap - state.bucketOne))); - } else { - adjacentStates.add(new State(state.moves + 1, state.bucketTwo + state.bucketOne, 0)); + if (bucketTwo != 0) { + fillBuckets(bucketOne, bucketOneCap, 0, bucketTwoCap, desiredLiters, actionsTaken + 1, startingBucket); } - return adjacentStates; - } - - private boolean isValid(State state) { - if (state.bucketOne == bucketOneCap && state.bucketTwo == 0 && startBucket.equals("two")) { - return false; - } else if (state.bucketOne == 0 && state.bucketTwo == bucketTwoCap && startBucket.equals("two")) { - return false; + fillBuckets( + bucketOneCap, bucketOneCap, bucketTwo, bucketTwoCap, desiredLiters, + actionsTaken + 1, startingBucket + ); + fillBuckets( + bucketOne, bucketOneCap, bucketTwoCap, bucketTwoCap, desiredLiters, + actionsTaken + 1, startingBucket + ); + + if (bucketOne + bucketTwo <= bucketTwoCap) { + fillBuckets( + 0, bucketOneCap, bucketTwo + bucketOne, bucketTwoCap, desiredLiters, + actionsTaken + 1, startingBucket + ); } else { - return true; + fillBuckets( + bucketOne + bucketTwo - bucketTwoCap, bucketOneCap, bucketTwoCap, bucketTwoCap, + desiredLiters, actionsTaken + 1, startingBucket + ); } - } - - private State computeFinalState() { - ArrayList paths = new ArrayList(); - - State initialState; - if (startBucket.equals("one")) { - initialState = new State(1, bucketOneCap, 0); + if (bucketOne + bucketTwo <= bucketOneCap) { + fillBuckets( + bucketOne + bucketTwo, bucketOneCap, 0, bucketTwoCap, desiredLiters, + actionsTaken + 1, startingBucket + ); } else { - initialState = new State(1, 0, bucketTwoCap); + fillBuckets( + bucketOneCap, bucketOneCap, bucketTwo + bucketOne - bucketOneCap, bucketTwoCap, + desiredLiters, actionsTaken + 1, startingBucket + ); } + } - if (initialState.bucketOne == desiredLiters || initialState.bucketTwo == desiredLiters) { - return initialState; + private void checkIfImpossible() { + boolean exceedsCapacity = desiredLiters > bucketOneCap && desiredLiters > bucketTwoCap; + boolean invalidDivision = desiredLiters % gcd(bucketOneCap, bucketTwoCap) != 0; + + if (exceedsCapacity || invalidDivision) { + throw new IllegalArgumentException("impossible"); } + } - paths.add(initialState); - - for (int i = 0; i < 10000; i++) { - State currentState = paths.remove(0); - ArrayList adjacentStates = getAdjacentStates(currentState); - for (State state : adjacentStates) { - if (state.bucketOne == desiredLiters || state.bucketTwo == desiredLiters) { - return state; - } - - if (!paths.contains(state) && isValid(state)) { - paths.add(state); - } - } + private int gcd(int a, int b) { + while (b != 0) { + int temp = b; + b = a % b; + a = temp; } - - return null; + return a; } int getTotalMoves() { - return finalState.moves; + checkIfImpossible(); + return totalMoves; } String getFinalBucket() { - if (finalState.bucketOne == desiredLiters) { - return "one"; - } else if (finalState.bucketTwo == desiredLiters) { - return "two"; - } else { - return "No solution found in " + finalState.moves + " iterations!"; - } + checkIfImpossible(); + return finalBucket; } int getOtherBucket() { - if (getFinalBucket().equals("one")) { - return finalState.bucketTwo; - } else if (getFinalBucket().equals("two")) { - return finalState.bucketOne; - } else { - return -1; - } + checkIfImpossible(); + return otherBucket; } } diff --git a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java index cd0b25612..7fbb5d6e8 100644 --- a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java +++ b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java @@ -4,8 +4,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import java.util.Collections; - public class TwoBucketTest { @Test From 916eef473b6215802151817476201db0cccfcadc Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 14 Jan 2025 23:19:19 +0530 Subject: [PATCH 06/10] Incorporating Result class in solution and example --- .../practice/two-bucket/.meta/config.json | 6 +- .../.meta/src/reference/java/Result.java | 24 ++++++ .../.meta/src/reference/java/TwoBucket.java | 33 ++------ .../two-bucket/src/main/java/Result.java | 25 ++++++ .../two-bucket/src/main/java/TwoBucket.java | 14 +--- .../src/test/java/TwoBucketTest.java | 78 ++++++++----------- 6 files changed, 96 insertions(+), 84 deletions(-) create mode 100644 exercises/practice/two-bucket/.meta/src/reference/java/Result.java create mode 100644 exercises/practice/two-bucket/src/main/java/Result.java diff --git a/exercises/practice/two-bucket/.meta/config.json b/exercises/practice/two-bucket/.meta/config.json index 65fb5a153..b4cb04746 100644 --- a/exercises/practice/two-bucket/.meta/config.json +++ b/exercises/practice/two-bucket/.meta/config.json @@ -17,13 +17,15 @@ ], "files": { "solution": [ - "src/main/java/TwoBucket.java" + "src/main/java/TwoBucket.java", + "src/main/java/Result.java" ], "test": [ "src/test/java/TwoBucketTest.java" ], "example": [ - ".meta/src/reference/java/TwoBucket.java" + ".meta/src/reference/java/TwoBucket.java", + ".meta/src/reference/java/Result.java" ], "invalidator": [ "build.gradle" diff --git a/exercises/practice/two-bucket/.meta/src/reference/java/Result.java b/exercises/practice/two-bucket/.meta/src/reference/java/Result.java new file mode 100644 index 000000000..b7b23ce64 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/src/reference/java/Result.java @@ -0,0 +1,24 @@ +final class Result { + + private final int totalMoves; + private final String finalBucket; + private final int otherBucket; + + Result(int totalMoves, String finalBuckets, int otherBucket) { + this.totalMoves = totalMoves; + this.finalBucket = finalBuckets; + this.otherBucket = otherBucket; + } + + public int getTotalMoves() { + return totalMoves; + } + + public String getFinalBucket() { + return finalBucket; + } + + public int getOtherBucket() { + return otherBucket; + } +} diff --git a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java index a60431e2f..1adddc0d9 100644 --- a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java +++ b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java @@ -2,16 +2,16 @@ import java.util.List; import java.util.Map; -class TwoBucket { +final class TwoBucket { private int totalMoves = Integer.MAX_VALUE; private String finalBucket = ""; private int otherBucket = Integer.MAX_VALUE; - private final int bucketOneCap; - private final int bucketTwoCap; - private final int desiredLiters; - + private List> statesReached = new ArrayList<>(); + TwoBucket(int bucketOneCap, int bucketTwoCap, int desiredLiters, String startBucket) { + checkIfImpossible(desiredLiters, bucketOneCap, bucketTwoCap); + fillBuckets( startBucket.equals("one") ? bucketOneCap : 0, bucketOneCap, @@ -21,14 +21,8 @@ class TwoBucket { 1, startBucket ); - - this.bucketOneCap = bucketOneCap; - this.bucketTwoCap = bucketTwoCap; - this.desiredLiters = desiredLiters; } - private List> statesReached = new ArrayList<>(); - private void fillBuckets( int bucketOne, int bucketOneCap, @@ -105,7 +99,7 @@ private void fillBuckets( } } - private void checkIfImpossible() { + private void checkIfImpossible(int desiredLiters, int bucketOneCap, int bucketTwoCap) { boolean exceedsCapacity = desiredLiters > bucketOneCap && desiredLiters > bucketTwoCap; boolean invalidDivision = desiredLiters % gcd(bucketOneCap, bucketTwoCap) != 0; @@ -123,18 +117,7 @@ private int gcd(int a, int b) { return a; } - int getTotalMoves() { - checkIfImpossible(); - return totalMoves; - } - - String getFinalBucket() { - checkIfImpossible(); - return finalBucket; - } - - int getOtherBucket() { - checkIfImpossible(); - return otherBucket; + Result getResult() { + return new Result(totalMoves, finalBucket, otherBucket); } } diff --git a/exercises/practice/two-bucket/src/main/java/Result.java b/exercises/practice/two-bucket/src/main/java/Result.java new file mode 100644 index 000000000..04c5bd90a --- /dev/null +++ b/exercises/practice/two-bucket/src/main/java/Result.java @@ -0,0 +1,25 @@ +final class Result { + + private final int totalMoves; + private final String finalBucket; + private final int otherBucket; + + Result(int totalMoves, String finalBuckets, int otherBucket) { + this.totalMoves = totalMoves; + this.finalBucket = finalBuckets; + this.otherBucket = otherBucket; + } + + public int getTotalMoves() { + return totalMoves; + } + + public String getFinalBucket() { + return finalBucket; + } + + public int getOtherBucket() { + return otherBucket; + } + +} diff --git a/exercises/practice/two-bucket/src/main/java/TwoBucket.java b/exercises/practice/two-bucket/src/main/java/TwoBucket.java index ae7be705a..5cd0e4d87 100644 --- a/exercises/practice/two-bucket/src/main/java/TwoBucket.java +++ b/exercises/practice/two-bucket/src/main/java/TwoBucket.java @@ -1,17 +1,11 @@ class TwoBucket { + TwoBucket(int bucketOneCap, int bucketTwoCap, int desiredLiters, String startBucket) { throw new UnsupportedOperationException("Please implement the TwoBucket(int, int, int, String) constructor."); } - int getTotalMoves() { - throw new UnsupportedOperationException("Please implement the TwoBucket.getTotalMoves() method."); - } - - String getFinalBucket() { - throw new UnsupportedOperationException("Please implement the TwoBucket.getFinalBucket() method."); - } - - int getOtherBucket() { - throw new UnsupportedOperationException("Please implement the TwoBucket.getOtherBucket() method."); + Result getResult() { + throw new UnsupportedOperationException("Please implement the TwoBucket(int, int, int, String) constructor."); } + } diff --git a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java index 7fbb5d6e8..3bb86820b 100644 --- a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java +++ b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java @@ -9,11 +9,11 @@ public class TwoBucketTest { @Test public void testBucketOneSizeThreeBucketTwoSizeFiveStartWithOne() { - TwoBucket twoBucket = new TwoBucket(3, 5, 1, "one"); + Result bucketResult = new TwoBucket(3, 5, 1, "one").getResult(); - assertThat(twoBucket.getTotalMoves()).isEqualTo(4); - assertThat(twoBucket.getFinalBucket()).isEqualTo("one"); - assertThat(twoBucket.getOtherBucket()).isEqualTo(5); + assertThat(bucketResult.getTotalMoves()).isEqualTo(4); + assertThat(bucketResult.getFinalBucket()).isEqualTo("one"); + assertThat(bucketResult.getOtherBucket()).isEqualTo(5); } @@ -21,11 +21,11 @@ public void testBucketOneSizeThreeBucketTwoSizeFiveStartWithOne() { @Test public void testBucketOneSizeThreeBucketTwoSizeFiveStartWithTwo() { - TwoBucket twoBucket = new TwoBucket(3, 5, 1, "two"); + Result bucketResult = new TwoBucket(3, 5, 1, "two").getResult(); - assertThat(twoBucket.getTotalMoves()).isEqualTo(8); - assertThat(twoBucket.getFinalBucket()).isEqualTo("two"); - assertThat(twoBucket.getOtherBucket()).isEqualTo(3); + assertThat(bucketResult.getTotalMoves()).isEqualTo(8); + assertThat(bucketResult.getFinalBucket()).isEqualTo("two"); + assertThat(bucketResult.getOtherBucket()).isEqualTo(3); } @@ -33,11 +33,11 @@ public void testBucketOneSizeThreeBucketTwoSizeFiveStartWithTwo() { @Test public void testBucketOneSizeSevenBucketTwoSizeElevenStartWithOne() { - TwoBucket twoBucket = new TwoBucket(7, 11, 2, "one"); + Result bucketResult = new TwoBucket(7, 11, 2, "one").getResult(); - assertThat(twoBucket.getTotalMoves()).isEqualTo(14); - assertThat(twoBucket.getFinalBucket()).isEqualTo("one"); - assertThat(twoBucket.getOtherBucket()).isEqualTo(11); + assertThat(bucketResult.getTotalMoves()).isEqualTo(14); + assertThat(bucketResult.getFinalBucket()).isEqualTo("one"); + assertThat(bucketResult.getOtherBucket()).isEqualTo(11); } @@ -45,11 +45,11 @@ public void testBucketOneSizeSevenBucketTwoSizeElevenStartWithOne() { @Test public void testBucketOneSizeSevenBucketTwoSizeElevenStartWithTwo() { - TwoBucket twoBucket = new TwoBucket(7, 11, 2, "two"); + Result bucketResult = new TwoBucket(7, 11, 2, "two").getResult(); - assertThat(twoBucket.getTotalMoves()).isEqualTo(18); - assertThat(twoBucket.getFinalBucket()).isEqualTo("two"); - assertThat(twoBucket.getOtherBucket()).isEqualTo(7); + assertThat(bucketResult.getTotalMoves()).isEqualTo(18); + assertThat(bucketResult.getFinalBucket()).isEqualTo("two"); + assertThat(bucketResult.getOtherBucket()).isEqualTo(7); } @@ -57,11 +57,11 @@ public void testBucketOneSizeSevenBucketTwoSizeElevenStartWithTwo() { @Test public void testBucketOneSizeOneBucketTwoSizeThreeStartWithTwo() { - TwoBucket twoBucket = new TwoBucket(1, 3, 3, "two"); + Result bucketResult = new TwoBucket(1, 3, 3, "two").getResult(); - assertThat(twoBucket.getTotalMoves()).isEqualTo(1); - assertThat(twoBucket.getFinalBucket()).isEqualTo("two"); - assertThat(twoBucket.getOtherBucket()).isEqualTo(0); + assertThat(bucketResult.getTotalMoves()).isEqualTo(1); + assertThat(bucketResult.getFinalBucket()).isEqualTo("two"); + assertThat(bucketResult.getOtherBucket()).isEqualTo(0); } @@ -69,28 +69,20 @@ public void testBucketOneSizeOneBucketTwoSizeThreeStartWithTwo() { @Test public void testBucketOneSizeTwoBucketTwoSizeThreeStartWithOne() { - TwoBucket twoBucket = new TwoBucket(2, 3, 3, "one"); + Result bucketResult = new TwoBucket(2, 3, 3, "one").getResult(); - assertThat(twoBucket.getTotalMoves()).isEqualTo(2); - assertThat(twoBucket.getFinalBucket()).isEqualTo("two"); - assertThat(twoBucket.getOtherBucket()).isEqualTo(2); + assertThat(bucketResult.getTotalMoves()).isEqualTo(2); + assertThat(bucketResult.getFinalBucket()).isEqualTo("two"); + assertThat(bucketResult.getOtherBucket()).isEqualTo(2); } @Disabled("Remove to run test") @Test public void testReachingGoalIsImpossible() { - - TwoBucket twoBucket = new TwoBucket(6, 15, 5, "one"); - - assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getTotalMoves()) - .withMessage("impossible"); - assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getFinalBucket()) - .withMessage("impossible"); + assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getOtherBucket()) + .isThrownBy(() -> new TwoBucket(6, 15, 5, "one")) .withMessage("impossible"); } @@ -99,11 +91,11 @@ public void testReachingGoalIsImpossible() { @Test public void testBucketOneSizeSixBucketTwoSizeFifteenStartWithOne() { - TwoBucket twoBucket = new TwoBucket(6, 15, 9, "one"); + Result bucketResult = new TwoBucket(6, 15, 9, "one").getResult(); - assertThat(twoBucket.getTotalMoves()).isEqualTo(10); - assertThat(twoBucket.getFinalBucket()).isEqualTo("two"); - assertThat(twoBucket.getOtherBucket()).isEqualTo(0); + assertThat(bucketResult.getTotalMoves()).isEqualTo(10); + assertThat(bucketResult.getFinalBucket()).isEqualTo("two"); + assertThat(bucketResult.getOtherBucket()).isEqualTo(0); } @@ -111,16 +103,8 @@ public void testBucketOneSizeSixBucketTwoSizeFifteenStartWithOne() { @Test public void testGoalLargerThanBothBucketsIsImpossible() { - TwoBucket twoBucket = new TwoBucket(5, 7, 8, "one"); - - assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getTotalMoves()) - .withMessage("impossible"); - assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getFinalBucket()) - .withMessage("impossible"); assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> twoBucket.getOtherBucket()) + .isThrownBy(() -> new TwoBucket(5, 7, 8, "one")) .withMessage("impossible"); } From 3bacb8c1cdd5a581b4f4631bb175f105718d2b07 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 14 Jan 2025 23:32:07 +0530 Subject: [PATCH 07/10] Adding contributor and small change in example answer --- exercises/practice/two-bucket/.meta/config.json | 1 + .../practice/two-bucket/.meta/src/reference/java/TwoBucket.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/exercises/practice/two-bucket/.meta/config.json b/exercises/practice/two-bucket/.meta/config.json index b4cb04746..d1863f52f 100644 --- a/exercises/practice/two-bucket/.meta/config.json +++ b/exercises/practice/two-bucket/.meta/config.json @@ -6,6 +6,7 @@ "FridaTveit", "jagdish-15", "jmrunkle", + "kahgoh", "lemoncurry", "LoadingBG", "mirkoperillo", diff --git a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java index 1adddc0d9..5a1d8be53 100644 --- a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java +++ b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java @@ -7,7 +7,7 @@ final class TwoBucket { private String finalBucket = ""; private int otherBucket = Integer.MAX_VALUE; - private List> statesReached = new ArrayList<>(); + private final List> statesReached = new ArrayList<>(); TwoBucket(int bucketOneCap, int bucketTwoCap, int desiredLiters, String startBucket) { checkIfImpossible(desiredLiters, bucketOneCap, bucketTwoCap); From 9e47280e4aa84cbe107ab2988a48934e418c77cf Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 14 Jan 2025 23:45:58 +0530 Subject: [PATCH 08/10] Incorporating custom exception --- .../.meta/src/reference/java/TwoBucket.java | 2 +- .../reference/java/UnreachableGoalException.java | 14 ++++++++++++++ .../practice/two-bucket/src/main/java/Result.java | 1 - .../src/main/java/UnreachableGoalException.java | 14 ++++++++++++++ .../two-bucket/src/test/java/TwoBucketTest.java | 4 ++-- 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 exercises/practice/two-bucket/.meta/src/reference/java/UnreachableGoalException.java create mode 100644 exercises/practice/two-bucket/src/main/java/UnreachableGoalException.java diff --git a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java index 5a1d8be53..457365340 100644 --- a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java +++ b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java @@ -104,7 +104,7 @@ private void checkIfImpossible(int desiredLiters, int bucketOneCap, int bucketTw boolean invalidDivision = desiredLiters % gcd(bucketOneCap, bucketTwoCap) != 0; if (exceedsCapacity || invalidDivision) { - throw new IllegalArgumentException("impossible"); + throw new UnreachableGoalException("impossible"); } } diff --git a/exercises/practice/two-bucket/.meta/src/reference/java/UnreachableGoalException.java b/exercises/practice/two-bucket/.meta/src/reference/java/UnreachableGoalException.java new file mode 100644 index 000000000..2b3ccbc49 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/src/reference/java/UnreachableGoalException.java @@ -0,0 +1,14 @@ +public class UnreachableGoalException extends RuntimeException { + + public UnreachableGoalException() { + super(); + } + + public UnreachableGoalException(String message) { + super(message); + } + + public UnreachableGoalException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/exercises/practice/two-bucket/src/main/java/Result.java b/exercises/practice/two-bucket/src/main/java/Result.java index 04c5bd90a..2464dab5f 100644 --- a/exercises/practice/two-bucket/src/main/java/Result.java +++ b/exercises/practice/two-bucket/src/main/java/Result.java @@ -21,5 +21,4 @@ public String getFinalBucket() { public int getOtherBucket() { return otherBucket; } - } diff --git a/exercises/practice/two-bucket/src/main/java/UnreachableGoalException.java b/exercises/practice/two-bucket/src/main/java/UnreachableGoalException.java new file mode 100644 index 000000000..2b3ccbc49 --- /dev/null +++ b/exercises/practice/two-bucket/src/main/java/UnreachableGoalException.java @@ -0,0 +1,14 @@ +public class UnreachableGoalException extends RuntimeException { + + public UnreachableGoalException() { + super(); + } + + public UnreachableGoalException(String message) { + super(message); + } + + public UnreachableGoalException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java index 3bb86820b..ec9278d72 100644 --- a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java +++ b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java @@ -81,7 +81,7 @@ public void testBucketOneSizeTwoBucketTwoSizeThreeStartWithOne() { @Test public void testReachingGoalIsImpossible() { - assertThatExceptionOfType(IllegalArgumentException.class) + assertThatExceptionOfType(UnreachableGoalException.class) .isThrownBy(() -> new TwoBucket(6, 15, 5, "one")) .withMessage("impossible"); @@ -103,7 +103,7 @@ public void testBucketOneSizeSixBucketTwoSizeFifteenStartWithOne() { @Test public void testGoalLargerThanBothBucketsIsImpossible() { - assertThatExceptionOfType(IllegalArgumentException.class) + assertThatExceptionOfType(UnreachableGoalException.class) .isThrownBy(() -> new TwoBucket(5, 7, 8, "one")) .withMessage("impossible"); From 9935ee5702393d452650a90b6d172b4dfe8a2e9a Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 14 Jan 2025 23:48:56 +0530 Subject: [PATCH 09/10] Configuring config.json --- exercises/practice/two-bucket/.meta/config.json | 6 ++++-- .../two-bucket/.meta/src/reference/java/TwoBucket.java | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/exercises/practice/two-bucket/.meta/config.json b/exercises/practice/two-bucket/.meta/config.json index d1863f52f..4f45ff4a4 100644 --- a/exercises/practice/two-bucket/.meta/config.json +++ b/exercises/practice/two-bucket/.meta/config.json @@ -19,14 +19,16 @@ "files": { "solution": [ "src/main/java/TwoBucket.java", - "src/main/java/Result.java" + "src/main/java/Result.java", + "src/main/java/UnreachableGoalException.java" ], "test": [ "src/test/java/TwoBucketTest.java" ], "example": [ ".meta/src/reference/java/TwoBucket.java", - ".meta/src/reference/java/Result.java" + ".meta/src/reference/java/Result.java", + ".meta/src/reference/java/UnreachableGoalException.java" ], "invalidator": [ "build.gradle" diff --git a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java index 457365340..9d8d18e1e 100644 --- a/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java +++ b/exercises/practice/two-bucket/.meta/src/reference/java/TwoBucket.java @@ -3,6 +3,7 @@ import java.util.Map; final class TwoBucket { + private int totalMoves = Integer.MAX_VALUE; private String finalBucket = ""; private int otherBucket = Integer.MAX_VALUE; From 81210a0e0916f21cf0a9e55007df55a6af83df5b Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Wed, 15 Jan 2025 17:39:26 +0530 Subject: [PATCH 10/10] Chaning asseration to call getResult for impossible bucket --- .../practice/two-bucket/src/test/java/TwoBucketTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java index ec9278d72..36fbf21d8 100644 --- a/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java +++ b/exercises/practice/two-bucket/src/test/java/TwoBucketTest.java @@ -82,7 +82,7 @@ public void testBucketOneSizeTwoBucketTwoSizeThreeStartWithOne() { public void testReachingGoalIsImpossible() { assertThatExceptionOfType(UnreachableGoalException.class) - .isThrownBy(() -> new TwoBucket(6, 15, 5, "one")) + .isThrownBy(() -> new TwoBucket(6, 15, 5, "one").getResult()) .withMessage("impossible"); } @@ -104,7 +104,7 @@ public void testBucketOneSizeSixBucketTwoSizeFifteenStartWithOne() { public void testGoalLargerThanBothBucketsIsImpossible() { assertThatExceptionOfType(UnreachableGoalException.class) - .isThrownBy(() -> new TwoBucket(5, 7, 8, "one")) + .isThrownBy(() -> new TwoBucket(5, 7, 8, "one").getResult()) .withMessage("impossible"); }