From fd4b48cd58e3d823cb4dc7e1b1206960bc8f9273 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sat, 14 Oct 2023 13:02:25 +0530 Subject: [PATCH 1/8] Update AllCombinationsOfSizeK.js --- Backtracking/AllCombinationsOfSizeK.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Backtracking/AllCombinationsOfSizeK.js b/Backtracking/AllCombinationsOfSizeK.js index 264acb1f19..7f31e79533 100644 --- a/Backtracking/AllCombinationsOfSizeK.js +++ b/Backtracking/AllCombinationsOfSizeK.js @@ -27,18 +27,21 @@ class Combinations { this.k = k this.current = [] // will be used for storing current combination this.combinations = [] + this.i = 1 } - findCombinations(high = this.n, total = this.k, low = 1) { - if (total === 0) { - this.combinations.push([...this.current]) - return this.combinations - } - for (let i = low; i <= high; i++) { - this.current.push(i) - this.findCombinations(high, total - 1, i + 1) - this.current.pop() - } + findCombinations() { + if (this.current.length == this.k) { //will add the array of size k to combinations array + this.combinations.push([...this.current]) + return + } + if (this.i > this.n) //check for exceeding range + return + this.current.push(this.i++) + this.findCombinations() + this.current.pop() + this.findCombinations() + this.i-- return this.combinations } } From f4d6fedb96b796b6d4557e1f9d964b9f7aa25980 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:48:47 +0530 Subject: [PATCH 2/8] Update AllCombinationsOfSizeK.js --- Backtracking/AllCombinationsOfSizeK.js | 46 ++++++++++++++------------ 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/Backtracking/AllCombinationsOfSizeK.js b/Backtracking/AllCombinationsOfSizeK.js index 7f31e79533..2279e8241f 100644 --- a/Backtracking/AllCombinationsOfSizeK.js +++ b/Backtracking/AllCombinationsOfSizeK.js @@ -21,29 +21,31 @@ - Once we get all combinations of that element, pop it and do same for next element */ -class Combinations { - constructor(n, k) { - this.n = n - this.k = k - this.current = [] // will be used for storing current combination - this.combinations = [] - this.i = 1 +function generateCombinations(n, k) { + let currentCombination = [] + let allCombinations = [] // will be used for storing all combinations + let currentValue = 1 + + function findCombinations() { + if (currentCombination.length === k) { + // Add the array of size k to the allCombinations array + allCombinations.push([...currentCombination]) + return + } + if (currentValue > n) { + // Check for exceeding the range + return + } + currentCombination.push(currentValue++) + findCombinations() + currentCombination.pop() + findCombinations() + currentValue-- } - findCombinations() { - if (this.current.length == this.k) { //will add the array of size k to combinations array - this.combinations.push([...this.current]) - return - } - if (this.i > this.n) //check for exceeding range - return - this.current.push(this.i++) - this.findCombinations() - this.current.pop() - this.findCombinations() - this.i-- - return this.combinations - } + findCombinations() + + return allCombinations } -export { Combinations } +export { generateCombinations } From de46071908c994ded9badafd8d1cda416a06f590 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 16:20:02 +0530 Subject: [PATCH 3/8] Update AllCombinationsOfSizeK.test.js Changes made it the type of testing. Instead of testing the class now the program will test the function --- Backtracking/tests/AllCombinationsOfSizeK.test.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Backtracking/tests/AllCombinationsOfSizeK.test.js b/Backtracking/tests/AllCombinationsOfSizeK.test.js index a2135e54bf..b1e64f32d3 100644 --- a/Backtracking/tests/AllCombinationsOfSizeK.test.js +++ b/Backtracking/tests/AllCombinationsOfSizeK.test.js @@ -1,9 +1,8 @@ -import { Combinations } from '../AllCombinationsOfSizeK' +import { generateCombinations } from '../AllCombinationsOfSizeK' describe('AllCombinationsOfSizeK', () => { it('should return 3x2 matrix solution for n = 3 and k = 2', () => { - const test1 = new Combinations(3, 2) - expect(test1.findCombinations()).toEqual([ + expect(generateCombinations(3,2)).toEqual([ [1, 2], [1, 3], [2, 3] @@ -11,8 +10,7 @@ describe('AllCombinationsOfSizeK', () => { }) it('should return 6x2 matrix solution for n = 4 and k = 2', () => { - const test2 = new Combinations(4, 2) - expect(test2.findCombinations()).toEqual([ + expect(generateCombinations(4,2)).toEqual([ [1, 2], [1, 3], [1, 4], From 5a82cfd6e3dffcabc6f6a8c49924ce4ec1140bc9 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 18:43:18 +0530 Subject: [PATCH 4/8] Update AllCombinationsOfSizeK.js --- Backtracking/AllCombinationsOfSizeK.js | 29 +++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Backtracking/AllCombinationsOfSizeK.js b/Backtracking/AllCombinationsOfSizeK.js index 2279e8241f..145afbd227 100644 --- a/Backtracking/AllCombinationsOfSizeK.js +++ b/Backtracking/AllCombinationsOfSizeK.js @@ -20,32 +20,31 @@ - Take one element, and make all them combinations for k-1 elements - Once we get all combinations of that element, pop it and do same for next element */ - function generateCombinations(n, k) { - let currentCombination = [] - let allCombinations = [] // will be used for storing all combinations - let currentValue = 1 + let currentCombination = []; + let allCombinations = []; // will be used for storing all combinations + let currentValue = 1; function findCombinations() { if (currentCombination.length === k) { // Add the array of size k to the allCombinations array - allCombinations.push([...currentCombination]) - return + allCombinations.push([...currentCombination]); + return; } if (currentValue > n) { // Check for exceeding the range - return + return; } - currentCombination.push(currentValue++) - findCombinations() - currentCombination.pop() - findCombinations() - currentValue-- + currentCombination.push(currentValue++); + findCombinations(); + currentCombination.pop(); + findCombinations(); + currentValue--; } - findCombinations() + findCombinations(); - return allCombinations + return allCombinations; } -export { generateCombinations } +export { generateCombinations }; From 56f3199438b757efd56e7cb0e8345da6d48456c6 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 18:49:54 +0530 Subject: [PATCH 5/8] Update AllCombinationsOfSizeK.js --- Backtracking/AllCombinationsOfSizeK.js | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/Backtracking/AllCombinationsOfSizeK.js b/Backtracking/AllCombinationsOfSizeK.js index 145afbd227..1f13723cad 100644 --- a/Backtracking/AllCombinationsOfSizeK.js +++ b/Backtracking/AllCombinationsOfSizeK.js @@ -1,25 +1,3 @@ -/* - Problem: Given two numbers, n and k, make all unique combinations of k numbers from 1 to n and in sorted order - - What is combinations? - - Combinations is selecting items from a collections without considering the order of selection - - Example: - - We have an apple, a banana, and a jackfruit - - We have three objects, and need to choose two items, then combinations will be - - 1. Apple & Banana - 2. Apple & Jackfruit - 3. Banana & Jackfruit - - To read more about combinations, you can visit the following link: - - https://betterexplained.com/articles/easy-permutations-and-combinations/ - - Solution: - - We will be using backtracking to solve this questions - - Take one element, and make all them combinations for k-1 elements - - Once we get all combinations of that element, pop it and do same for next element -*/ function generateCombinations(n, k) { let currentCombination = []; let allCombinations = []; // will be used for storing all combinations From d5bb90cf565951d7789c4afa70555e70c7d6c378 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 19:03:18 +0530 Subject: [PATCH 6/8] Update AllCombinationsOfSizeK.js --- Backtracking/AllCombinationsOfSizeK.js | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Backtracking/AllCombinationsOfSizeK.js b/Backtracking/AllCombinationsOfSizeK.js index 1f13723cad..bbe4e7fd5b 100644 --- a/Backtracking/AllCombinationsOfSizeK.js +++ b/Backtracking/AllCombinationsOfSizeK.js @@ -1,28 +1,28 @@ function generateCombinations(n, k) { - let currentCombination = []; - let allCombinations = []; // will be used for storing all combinations - let currentValue = 1; + let currentCombination = [] + let allCombinations = [] // will be used for storing all combinations + let currentValue = 1 function findCombinations() { if (currentCombination.length === k) { // Add the array of size k to the allCombinations array - allCombinations.push([...currentCombination]); - return; + allCombinations.push([...currentCombination]) + return } if (currentValue > n) { // Check for exceeding the range - return; + return } - currentCombination.push(currentValue++); - findCombinations(); - currentCombination.pop(); - findCombinations(); - currentValue--; + currentCombination.push(currentValue++) + findCombinations() + currentCombination.pop() + findCombinations() + currentValue-- } - findCombinations(); + findCombinations() - return allCombinations; + return allCombinations } -export { generateCombinations }; +export { generateCombinations } From a63bf463dad9caa755cec115094b17f0936514a9 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 19:08:06 +0530 Subject: [PATCH 7/8] Update AllCombinationsOfSizeK.test.js --- Backtracking/tests/AllCombinationsOfSizeK.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Backtracking/tests/AllCombinationsOfSizeK.test.js b/Backtracking/tests/AllCombinationsOfSizeK.test.js index b1e64f32d3..b1f7a3c7f6 100644 --- a/Backtracking/tests/AllCombinationsOfSizeK.test.js +++ b/Backtracking/tests/AllCombinationsOfSizeK.test.js @@ -2,7 +2,8 @@ import { generateCombinations } from '../AllCombinationsOfSizeK' describe('AllCombinationsOfSizeK', () => { it('should return 3x2 matrix solution for n = 3 and k = 2', () => { - expect(generateCombinations(3,2)).toEqual([ + const res = generateCombinations(3,2) + expect(res).toEqual([ [1, 2], [1, 3], [2, 3] @@ -10,7 +11,8 @@ describe('AllCombinationsOfSizeK', () => { }) it('should return 6x2 matrix solution for n = 4 and k = 2', () => { - expect(generateCombinations(4,2)).toEqual([ + const res = generateCombinations(4,2) + expect(res).toEqual([ [1, 2], [1, 3], [1, 4], From 26008d2ac4ca78c6ef0236eccac76ce25a9fc9e0 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 22:59:59 +0530 Subject: [PATCH 8/8] Update AllCombinationsOfSizeK.test.js --- Backtracking/tests/AllCombinationsOfSizeK.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Backtracking/tests/AllCombinationsOfSizeK.test.js b/Backtracking/tests/AllCombinationsOfSizeK.test.js index b1f7a3c7f6..29b656a2c4 100644 --- a/Backtracking/tests/AllCombinationsOfSizeK.test.js +++ b/Backtracking/tests/AllCombinationsOfSizeK.test.js @@ -2,7 +2,7 @@ import { generateCombinations } from '../AllCombinationsOfSizeK' describe('AllCombinationsOfSizeK', () => { it('should return 3x2 matrix solution for n = 3 and k = 2', () => { - const res = generateCombinations(3,2) + const res = generateCombinations(3, 2) expect(res).toEqual([ [1, 2], [1, 3], @@ -11,7 +11,7 @@ describe('AllCombinationsOfSizeK', () => { }) it('should return 6x2 matrix solution for n = 4 and k = 2', () => { - const res = generateCombinations(4,2) + const res = generateCombinations(4, 2) expect(res).toEqual([ [1, 2], [1, 3],