From 844c1271250fd383d1f8a31b8503d14333190993 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:10:58 +0530 Subject: [PATCH 01/15] Create Water_Jug_Problem.js add the water jug problem solving function --- Recursive/Water_Jug_Problem.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Recursive/Water_Jug_Problem.js diff --git a/Recursive/Water_Jug_Problem.js b/Recursive/Water_Jug_Problem.js new file mode 100644 index 0000000000..3f7940dec7 --- /dev/null +++ b/Recursive/Water_Jug_Problem.js @@ -0,0 +1,34 @@ +function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) { + // 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; +} + From ff16d60673820a2b31f17c15e30393dfafc28dd1 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:22:30 +0530 Subject: [PATCH 02/15] Update Water_Jug_Problem.js formated the code with prettier --- Recursive/Water_Jug_Problem.js | 61 +++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/Recursive/Water_Jug_Problem.js b/Recursive/Water_Jug_Problem.js index 3f7940dec7..c7a1ff1912 100644 --- a/Recursive/Water_Jug_Problem.js +++ b/Recursive/Water_Jug_Problem.js @@ -1,34 +1,43 @@ function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) { - // Base case: If the target amount is greater than the sum of both jugs, it's not possible. - if (targetAmount > jug1Capacity + jug2Capacity) return false; + // 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. + // 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(); + 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 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); + // 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 - } + // 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; + // If no solution is found + return false; } - From 50825fae5c0a4684d2d532a3c8c3ab3514729f3e Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:31:29 +0530 Subject: [PATCH 03/15] Create WaterJugProblem.test.js test cases for water jug problem --- Recursive/test/WaterJugProblem.test.js | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Recursive/test/WaterJugProblem.test.js diff --git a/Recursive/test/WaterJugProblem.test.js b/Recursive/test/WaterJugProblem.test.js new file mode 100644 index 0000000000..90113cf221 --- /dev/null +++ b/Recursive/test/WaterJugProblem.test.js @@ -0,0 +1,27 @@ +const canMeasureWater = require('../Recursive/WaterJugProblem'); + +describe('Water Jug Problem', () => { + test('should return true when target amount is achievable', () => { + expect(canMeasureWater(3, 5, 4)).toBe(true); // Known solution exists + }); + + test('should return false when target amount is not achievable', () => { + expect(canMeasureWater(2, 6, 5)).toBe(false); // Impossible to measure 5 using 2 and 6 + }); + + test('should return true when one of the jugs exactly matches the target', () => { + expect(canMeasureWater(3, 5, 5)).toBe(true); // Exact match with jug 2 + }); + + test('should return true when both jugs are enough to make the target', () => { + expect(canMeasureWater(4, 3, 7)).toBe(true); // Combined total equals target + }); + + test('should return false when target amount exceeds both jug capacities combined', () => { + expect(canMeasureWater(3, 5, 9)).toBe(false); // 9 exceeds the total capacity (3 + 5) + }); + + test('should return true for zero target', () => { + expect(canMeasureWater(3, 5, 0)).toBe(true); // It's always possible to measure 0 + }); +}); From db73ccf7c7ff4af7fba4406e6edddbcd1c3760ad Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:34:57 +0530 Subject: [PATCH 04/15] Update WaterJugProblem.test.js --- Recursive/test/WaterJugProblem.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Recursive/test/WaterJugProblem.test.js b/Recursive/test/WaterJugProblem.test.js index 90113cf221..c71142939c 100644 --- a/Recursive/test/WaterJugProblem.test.js +++ b/Recursive/test/WaterJugProblem.test.js @@ -1,4 +1,4 @@ -const canMeasureWater = require('../Recursive/WaterJugProblem'); +const canMeasureWater = require('../Recursive/Water_Jug_Problem'); describe('Water Jug Problem', () => { test('should return true when target amount is achievable', () => { From 77c1aaaacd1ae6bc96f595898e8c2a6c93e5c546 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:39:42 +0530 Subject: [PATCH 05/15] Update WaterJugProblem.test.js --- Recursive/test/WaterJugProblem.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Recursive/test/WaterJugProblem.test.js b/Recursive/test/WaterJugProblem.test.js index c71142939c..44b5ce725a 100644 --- a/Recursive/test/WaterJugProblem.test.js +++ b/Recursive/test/WaterJugProblem.test.js @@ -1,4 +1,4 @@ -const canMeasureWater = require('../Recursive/Water_Jug_Problem'); +const canMeasureWater = require('../Recursive/Water_jug_Problem'); describe('Water Jug Problem', () => { test('should return true when target amount is achievable', () => { From a7ed8dcefbc73a99855db911eb8619804a8593f4 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:41:54 +0530 Subject: [PATCH 06/15] Update WaterJugProblem.test.js --- Recursive/test/WaterJugProblem.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Recursive/test/WaterJugProblem.test.js b/Recursive/test/WaterJugProblem.test.js index 44b5ce725a..206e8b6716 100644 --- a/Recursive/test/WaterJugProblem.test.js +++ b/Recursive/test/WaterJugProblem.test.js @@ -1,4 +1,4 @@ -const canMeasureWater = require('../Recursive/Water_jug_Problem'); +const canMeasureWater = require('./Recursive/Water_jug_Problem'); describe('Water Jug Problem', () => { test('should return true when target amount is achievable', () => { From 801188bd14cca5c7315f22d58424fe626cbfc447 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:45:15 +0530 Subject: [PATCH 07/15] Update WaterJugProblem.test.js --- Recursive/test/WaterJugProblem.test.js | 59 +++++++++++++++++--------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/Recursive/test/WaterJugProblem.test.js b/Recursive/test/WaterJugProblem.test.js index 206e8b6716..dc8d55481b 100644 --- a/Recursive/test/WaterJugProblem.test.js +++ b/Recursive/test/WaterJugProblem.test.js @@ -1,27 +1,46 @@ -const canMeasureWater = require('./Recursive/Water_jug_Problem'); +import { canMeasureWater } from '../WaterJugProblem' describe('Water Jug Problem', () => { - test('should return true when target amount is achievable', () => { - expect(canMeasureWater(3, 5, 4)).toBe(true); // Known solution exists - }); + + // Test case 1: Valid input where water can be measured + it('should return true for values x=3, y=5, z=4', () => { + expect(canMeasureWater(3, 5, 4)).toBe(true) + }) - test('should return false when target amount is not achievable', () => { - expect(canMeasureWater(2, 6, 5)).toBe(false); // Impossible to measure 5 using 2 and 6 - }); + // Test case 2: Valid input where water cannot be measured + it('should return false for values x=2, y=6, z=5', () => { + expect(canMeasureWater(2, 6, 5)).toBe(false) + }) - test('should return true when one of the jugs exactly matches the target', () => { - expect(canMeasureWater(3, 5, 5)).toBe(true); // Exact match with jug 2 - }); + // Test case 3: Measure exact amount of water in one jug + it('should return true for values x=5, y=3, z=5', () => { + expect(canMeasureWater(5, 3, 5)).toBe(true) + }) - test('should return true when both jugs are enough to make the target', () => { - expect(canMeasureWater(4, 3, 7)).toBe(true); // Combined total equals target - }); + // Test case 4: Invalid inputs (negative or non-integer) + it('Throw Error for Invalid Input', () => { + expect(() => canMeasureWater(-3, 5, 4)).toThrow( + 'Input should be non-negative whole numbers' + ) + expect(() => canMeasureWater(3, null, 4)).toThrow( + 'Input should be non-negative whole numbers' + ) + expect(() => canMeasureWater(3, 5, 2.5)).toThrow( + 'Input should be non-negative whole numbers' + ) + expect(() => canMeasureWater('a', 5, 4)).toThrow( + 'Input should be non-negative whole numbers' + ) + }) - test('should return false when target amount exceeds both jug capacities combined', () => { - expect(canMeasureWater(3, 5, 9)).toBe(false); // 9 exceeds the total capacity (3 + 5) - }); + // Test case 5: Edge case where z is 0 + it('should return true for values x=3, y=5, z=0', () => { + expect(canMeasureWater(3, 5, 0)).toBe(true) + }) - test('should return true for zero target', () => { - expect(canMeasureWater(3, 5, 0)).toBe(true); // It's always possible to measure 0 - }); -}); + // Test case 6: Edge case where z equals the sum of both jugs + it('should return true for values x=3, y=5, z=8', () => { + expect(canMeasureWater(3, 5, 8)).toBe(true) + }) + +}) From 994a8502a7ac4a76ff6cba66332fac0ae6c06283 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:45:34 +0530 Subject: [PATCH 08/15] Rename Water_Jug_Problem.js to WaterJugProblem.js --- Recursive/{Water_Jug_Problem.js => WaterJugProblem.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Recursive/{Water_Jug_Problem.js => WaterJugProblem.js} (100%) diff --git a/Recursive/Water_Jug_Problem.js b/Recursive/WaterJugProblem.js similarity index 100% rename from Recursive/Water_Jug_Problem.js rename to Recursive/WaterJugProblem.js From 8fb13a2a98c2b758213d948ffdf4e87165c925ba Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:49:09 +0530 Subject: [PATCH 09/15] Update WaterJugProblem.js --- Recursive/WaterJugProblem.js | 73 ++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/Recursive/WaterJugProblem.js b/Recursive/WaterJugProblem.js index c7a1ff1912..b0b9a7df3b 100644 --- a/Recursive/WaterJugProblem.js +++ b/Recursive/WaterJugProblem.js @@ -1,43 +1,44 @@ -function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) { - // Base case: If the target amount is greater than the sum of both jugs, it's not possible. - if (targetAmount > jug1Capacity + jug2Capacity) return false; +// WaterJugProblem.js - // 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. +/** + * Function to determine if it is possible to measure exactly targetAmount + * using two jugs of capacity jug1Capacity and jug2Capacity. + * + * @param {number} jug1Capacity - Capacity of the first jug + * @param {number} jug2Capacity - Capacity of the second jug + * @param {number} targetAmount - Target amount of water + * @returns {boolean} + */ +export function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) { + // Base case: If the target amount is greater than the sum of both jugs, it's not possible. + if (targetAmount > jug1Capacity + jug2Capacity) return false; - while (queue.length > 0) { - let [jug1, jug2] = queue.shift(); + // 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. - // If we've reached the target amount in either jug. - if ( - jug1 === targetAmount || - jug2 === targetAmount || - jug1 + jug2 === targetAmount - ) { - return true; - } + 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); + // 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 - } + // 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; + // If no solution is found + return false; } From fcc6bdea764728a0d7d5a1f165d1d96e483150dd Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:49:38 +0530 Subject: [PATCH 10/15] Update WaterJugProblem.test.js --- Recursive/test/WaterJugProblem.test.js | 62 ++++++++++---------------- 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/Recursive/test/WaterJugProblem.test.js b/Recursive/test/WaterJugProblem.test.js index dc8d55481b..234a0af57f 100644 --- a/Recursive/test/WaterJugProblem.test.js +++ b/Recursive/test/WaterJugProblem.test.js @@ -1,46 +1,30 @@ -import { canMeasureWater } from '../WaterJugProblem' +// WaterJugProblem.test.js +import { canMeasureWater } from '../WaterJugProblem'; // Adjust the path as necessary describe('Water Jug Problem', () => { - - // Test case 1: Valid input where water can be measured - it('should return true for values x=3, y=5, z=4', () => { - expect(canMeasureWater(3, 5, 4)).toBe(true) - }) + it('should return true for values jug1=3, jug2=5, target=4', () => { + expect(canMeasureWater(3, 5, 4)).toBe(true); + }); - // Test case 2: Valid input where water cannot be measured - it('should return false for values x=2, y=6, z=5', () => { - expect(canMeasureWater(2, 6, 5)).toBe(false) - }) + it('should return false for values jug1=2, jug2=6, target=5', () => { + expect(canMeasureWater(2, 6, 5)).toBe(false); + }); - // Test case 3: Measure exact amount of water in one jug - it('should return true for values x=5, y=3, z=5', () => { - expect(canMeasureWater(5, 3, 5)).toBe(true) - }) + it('should return true for values jug1=5, jug2=3, target=5', () => { + expect(canMeasureWater(5, 3, 5)).toBe(true); + }); - // Test case 4: Invalid inputs (negative or non-integer) - it('Throw Error for Invalid Input', () => { - expect(() => canMeasureWater(-3, 5, 4)).toThrow( - 'Input should be non-negative whole numbers' - ) - expect(() => canMeasureWater(3, null, 4)).toThrow( - 'Input should be non-negative whole numbers' - ) - expect(() => canMeasureWater(3, 5, 2.5)).toThrow( - 'Input should be non-negative whole numbers' - ) - expect(() => canMeasureWater('a', 5, 4)).toThrow( - 'Input should be non-negative whole numbers' - ) - }) + it('should return true for values jug1=3, jug2=5, target=0', () => { + expect(canMeasureWater(3, 5, 0)).toBe(true); + }); - // Test case 5: Edge case where z is 0 - it('should return true for values x=3, y=5, z=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); + }); - // Test case 6: Edge case where z equals the sum of both jugs - it('should return true for values x=3, y=5, z=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.'); + }); +}); From acd529606c3f8746d904d9f20f7eecfe266cfafa Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:52:42 +0530 Subject: [PATCH 11/15] Update WaterJugProblem.js --- Recursive/WaterJugProblem.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Recursive/WaterJugProblem.js b/Recursive/WaterJugProblem.js index b0b9a7df3b..56d2e4889e 100644 --- a/Recursive/WaterJugProblem.js +++ b/Recursive/WaterJugProblem.js @@ -1,15 +1,14 @@ // WaterJugProblem.js -/** - * Function to determine if it is possible to measure exactly targetAmount - * using two jugs of capacity jug1Capacity and jug2Capacity. - * - * @param {number} jug1Capacity - Capacity of the first jug - * @param {number} jug2Capacity - Capacity of the second jug - * @param {number} targetAmount - Target amount of water - * @returns {boolean} - */ 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; From 74a65b7d02a557316df183f0167ba5b75bf9b405 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:53:53 +0530 Subject: [PATCH 12/15] Update WaterJugProblem.test.js --- Recursive/test/WaterJugProblem.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Recursive/test/WaterJugProblem.test.js b/Recursive/test/WaterJugProblem.test.js index 234a0af57f..5c0be2559d 100644 --- a/Recursive/test/WaterJugProblem.test.js +++ b/Recursive/test/WaterJugProblem.test.js @@ -26,5 +26,7 @@ describe('Water Jug Problem', () => { 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.'); + + }); }); From 3ab1fcbe198b7968d2d9f643576c69e8b4fec9e1 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 20:00:12 +0530 Subject: [PATCH 13/15] Update WaterJugProblem.js prettier --- Recursive/WaterJugProblem.js | 84 ++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/Recursive/WaterJugProblem.js b/Recursive/WaterJugProblem.js index 56d2e4889e..348d9684f9 100644 --- a/Recursive/WaterJugProblem.js +++ b/Recursive/WaterJugProblem.js @@ -1,43 +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 + // 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 no solution is found - return false; + // 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; } From e2ebc2288dd6c93445a8d700d63dea82906a88a2 Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 20:00:52 +0530 Subject: [PATCH 14/15] Update WaterJugProblem.test.js --- Recursive/test/WaterJugProblem.test.js | 52 ++++++++++++++------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/Recursive/test/WaterJugProblem.test.js b/Recursive/test/WaterJugProblem.test.js index 5c0be2559d..7f068e4d40 100644 --- a/Recursive/test/WaterJugProblem.test.js +++ b/Recursive/test/WaterJugProblem.test.js @@ -1,32 +1,36 @@ // WaterJugProblem.test.js -import { canMeasureWater } from '../WaterJugProblem'; // Adjust the path as necessary +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); - }); +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 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=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=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 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.'); - - - }); + 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." + ); + }); }); From 12aab081446ecd8cc9033dc5ca02bda724ee898f Mon Sep 17 00:00:00 2001 From: Swapnilkumar Dwivedi <122719184+swappy-2003@users.noreply.github.com> Date: Thu, 24 Oct 2024 20:01:56 +0530 Subject: [PATCH 15/15] Update WaterJugProblem.test.js