Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:master
base:master
Choose a base branch
Loading
fromswappy-2003:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
15 commits
Select commitHold shift + click to select a range
844c127
Create Water_Jug_Problem.js
swappy-2003Oct 24, 2024
ff16d60
Update Water_Jug_Problem.js
swappy-2003Oct 24, 2024
50825fa
Create WaterJugProblem.test.js
swappy-2003Oct 24, 2024
db73ccf
Update WaterJugProblem.test.js
swappy-2003Oct 24, 2024
77c1aaa
Update WaterJugProblem.test.js
swappy-2003Oct 24, 2024
a7ed8dc
Update WaterJugProblem.test.js
swappy-2003Oct 24, 2024
801188b
Update WaterJugProblem.test.js
swappy-2003Oct 24, 2024
994a850
Rename Water_Jug_Problem.js to WaterJugProblem.js
swappy-2003Oct 24, 2024
8fb13a2
Update WaterJugProblem.js
swappy-2003Oct 24, 2024
fcc6bde
Update WaterJugProblem.test.js
swappy-2003Oct 24, 2024
acd5296
Update WaterJugProblem.js
swappy-2003Oct 24, 2024
74a65b7
Update WaterJugProblem.test.js
swappy-2003Oct 24, 2024
3ab1fcb
Update WaterJugProblem.js
swappy-2003Oct 24, 2024
e2ebc22
Update WaterJugProblem.test.js
swappy-2003Oct 24, 2024
12aab08
Update WaterJugProblem.test.js
swappy-2003Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletionsRecursive/WaterJugProblem.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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;
}
36 changes: 36 additions & 0 deletionsRecursive/test/WaterJugProblem.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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."
);
});
});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp