Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
Create Water_Jug_Problem.js function in the Recursive section#1751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
swappy-2003 wants to merge15 commits intoTheAlgorithms:masterChoose a base branch fromswappy-2003:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
15 commits Select commitHold shift + click to select a range
844c127
Create Water_Jug_Problem.js
swappy-2003ff16d60
Update Water_Jug_Problem.js
swappy-200350825fa
Create WaterJugProblem.test.js
swappy-2003db73ccf
Update WaterJugProblem.test.js
swappy-200377c1aaa
Update WaterJugProblem.test.js
swappy-2003a7ed8dc
Update WaterJugProblem.test.js
swappy-2003801188b
Update WaterJugProblem.test.js
swappy-2003994a850
Rename Water_Jug_Problem.js to WaterJugProblem.js
swappy-20038fb13a2
Update WaterJugProblem.js
swappy-2003fcc6bde
Update WaterJugProblem.test.js
swappy-2003acd5296
Update WaterJugProblem.js
swappy-200374a65b7
Update WaterJugProblem.test.js
swappy-20033ab1fcb
Update WaterJugProblem.js
swappy-2003e2ebc22
Update WaterJugProblem.test.js
swappy-200312aab08
Update WaterJugProblem.test.js
swappy-2003File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// WaterJugProblem.js | ||
export function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) { | ||
// Input validation | ||
if (jug1Capacity < 0 || jug2Capacity < 0) { | ||
throw new Error("Invalid input: capacities must be non-negative."); | ||
} | ||
if (targetAmount < 0) { | ||
throw new Error("Invalid input: target amount must be non-negative."); | ||
} | ||
// Base case: If the target amount is greater than the sum of both jugs, it's not possible. | ||
if (targetAmount > jug1Capacity + jug2Capacity) return false; | ||
// Use BFS to explore possible states. | ||
let visited = new Set(); // To keep track of visited states. | ||
let queue = [[0, 0]]; // Starting with both jugs empty. | ||
while (queue.length > 0) { | ||
let [jug1, jug2] = queue.shift(); | ||
// If we've reached the target amount in either jug. | ||
if ( | ||
jug1 === targetAmount || | ||
jug2 === targetAmount || | ||
jug1 + jug2 === targetAmount | ||
) { | ||
return true; | ||
} | ||
// If this state has been visited, skip it. | ||
let state = `${jug1},${jug2}`; | ||
if (visited.has(state)) continue; | ||
visited.add(state); | ||
// Add all possible next states to the queue: | ||
queue.push([jug1Capacity, jug2]); // Fill Jug 1 | ||
queue.push([jug1, jug2Capacity]); // Fill Jug 2 | ||
queue.push([0, jug2]); // Empty Jug 1 | ||
queue.push([jug1, 0]); // Empty Jug 2 | ||
queue.push([ | ||
Math.max(0, jug1 - (jug2Capacity - jug2)), | ||
Math.min(jug2Capacity, jug1 + jug2), | ||
]); // Pour Jug 1 into Jug 2 | ||
queue.push([ | ||
Math.min(jug1Capacity, jug1 + jug2), | ||
Math.max(0, jug2 - (jug1Capacity - jug1)), | ||
]); // Pour Jug 2 into Jug 1 | ||
} | ||
// If no solution is found | ||
return false; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// WaterJugProblem.test.js | ||
import { canMeasureWater } from "../WaterJugProblem"; // Adjust the path as necessary | ||
describe("Water Jug Problem", () => { | ||
it("should return true for values jug1=3, jug2=5, target=4", () => { | ||
expect(canMeasureWater(3, 5, 4)).toBe(true); | ||
}); | ||
it("should return false for values jug1=2, jug2=6, target=5", () => { | ||
expect(canMeasureWater(2, 6, 5)).toBe(false); | ||
}); | ||
it("should return true for values jug1=5, jug2=3, target=5", () => { | ||
expect(canMeasureWater(5, 3, 5)).toBe(true); | ||
}); | ||
it("should return true for values jug1=3, jug2=5, target=0", () => { | ||
expect(canMeasureWater(3, 5, 0)).toBe(true); | ||
}); | ||
it("should return true for values jug1=3, jug2=5, target=8", () => { | ||
expect(canMeasureWater(3, 5, 8)).toBe(true); | ||
}); | ||
it("should throw an error for invalid input", () => { | ||
expect(() => canMeasureWater(-1, 5, 3)).toThrow( | ||
"Invalid input: capacities must be non-negative." | ||
); | ||
expect(() => canMeasureWater(3, -2, 1)).toThrow( | ||
"Invalid input: capacities must be non-negative." | ||
); | ||
expect(() => canMeasureWater(3, 5, -1)).toThrow( | ||
"Invalid input: target amount must be non-negative." | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.