From cf9ad50b343ccbd9d8bad59878b53ea6d85bd73d Mon Sep 17 00:00:00 2001 From: Nikunj Bisht Date: Wed, 11 Oct 2023 21:40:23 +0530 Subject: [PATCH 1/6] feat: added find subsets algorithm using bitmanipulation --- Bit-Manipulation/generateSubSets.js | 31 +++++++++++++++++++ Bit-Manipulation/test/generateSubSets.test.js | 17 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 Bit-Manipulation/generateSubSets.js create mode 100644 Bit-Manipulation/test/generateSubSets.test.js diff --git a/Bit-Manipulation/generateSubSets.js b/Bit-Manipulation/generateSubSets.js new file mode 100644 index 0000000000..70d2022a67 --- /dev/null +++ b/Bit-Manipulation/generateSubSets.js @@ -0,0 +1,31 @@ +/** + * @function generateSubSets + * @param {Array} inputArray + * @returns {Array} + * @example [1,2] -> [[],[1],[2],[1,2]] + */ + +// The time complexity of thhis algorithm is BigO(2^n) where n is the length of array +function generateSubSets(inputArray) { + if (!Array.isArray(inputArray)) { + return 'Input is not an array' + } + let arrayLength = inputArray.length + let subSets = [] + // loop till (2^n) - 1 + for (let i = 0; i < 1 << arrayLength; i++) { + let subSet = [] + for (let j = 0; j < arrayLength; j++) { + // 1 << j it shifts binary digit 1 by j positions and then we perform + // and by AND operation we are checking whetheer jth bit + // in i is set to 1 if result is non zero just add into set + if (i & (1 << j)) { + subSet.push(inputArray[j]) + } + } + subSets.push(subSet) + } + return subSets +} + +export { generateSubSets } diff --git a/Bit-Manipulation/test/generateSubSets.test.js b/Bit-Manipulation/test/generateSubSets.test.js new file mode 100644 index 0000000000..491d103f27 --- /dev/null +++ b/Bit-Manipulation/test/generateSubSets.test.js @@ -0,0 +1,17 @@ +import { generateSubSets } from '../generateSubSets' + +describe('subSets', () => { + it('find the subsets', () => { + expect(generateSubSets([1, 2, 3])).toEqual([ + [], + [1], + [2], + [1, 2], + [3], + [1, 3], + [2, 3], + [1, 2, 3] + ]) + expect(generateSubSets([1, 2])).toEqual([[], [1], [2], [1, 2]]) + }) +}) From aeca9a8fad33a84a0d3da73ee40513d03a529746 Mon Sep 17 00:00:00 2001 From: Nikunj Bisht Date: Wed, 11 Oct 2023 21:45:35 +0530 Subject: [PATCH 2/6] file name fix --- .../{generateSubSets.js => GenerateSubSets.js} | 0 Bit-Manipulation/test/generateSubSets.test.js | 2 +- Maths/test/EuclideanDistance.test.js | 10 ++++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) rename Bit-Manipulation/{generateSubSets.js => GenerateSubSets.js} (100%) diff --git a/Bit-Manipulation/generateSubSets.js b/Bit-Manipulation/GenerateSubSets.js similarity index 100% rename from Bit-Manipulation/generateSubSets.js rename to Bit-Manipulation/GenerateSubSets.js diff --git a/Bit-Manipulation/test/generateSubSets.test.js b/Bit-Manipulation/test/generateSubSets.test.js index 491d103f27..2abf578655 100644 --- a/Bit-Manipulation/test/generateSubSets.test.js +++ b/Bit-Manipulation/test/generateSubSets.test.js @@ -1,4 +1,4 @@ -import { generateSubSets } from '../generateSubSets' +import { generateSubSets } from '../GenerateSubSets' describe('subSets', () => { it('find the subsets', () => { diff --git a/Maths/test/EuclideanDistance.test.js b/Maths/test/EuclideanDistance.test.js index d73bb03875..717ea2e6a0 100644 --- a/Maths/test/EuclideanDistance.test.js +++ b/Maths/test/EuclideanDistance.test.js @@ -2,11 +2,17 @@ import { EuclideanDistance } from '../EuclideanDistance.js' describe('EuclideanDistance', () => { it('should calculate the distance correctly for 2D vectors', () => { - expect(EuclideanDistance([0, 0], [2, 2])).toBeCloseTo(2.8284271247461903, 10) + expect(EuclideanDistance([0, 0], [2, 2])).toBeCloseTo( + 2.8284271247461903, + 10 + ) }) it('should calculate the distance correctly for 3D vectors', () => { - expect(EuclideanDistance([0, 0, 0], [2, 2, 2])).toBeCloseTo(3.4641016151377544, 10) + expect(EuclideanDistance([0, 0, 0], [2, 2, 2])).toBeCloseTo( + 3.4641016151377544, + 10 + ) }) it('should calculate the distance correctly for 4D vectors', () => { From 3ae0a36869fbdf47041b488d3771ab079e06cd19 Mon Sep 17 00:00:00 2001 From: Nikunj Bisht Date: Wed, 11 Oct 2023 22:03:24 +0530 Subject: [PATCH 3/6] test file name fix --- .../test/{generateSubSets.test.js => GenerateSubSets.test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Bit-Manipulation/test/{generateSubSets.test.js => GenerateSubSets.test.js} (100%) diff --git a/Bit-Manipulation/test/generateSubSets.test.js b/Bit-Manipulation/test/GenerateSubSets.test.js similarity index 100% rename from Bit-Manipulation/test/generateSubSets.test.js rename to Bit-Manipulation/test/GenerateSubSets.test.js From cdf1ca28141ee7043622a4b16fd019d391be9fbd Mon Sep 17 00:00:00 2001 From: Nikunj Bisht Date: Wed, 11 Oct 2023 22:27:24 +0530 Subject: [PATCH 4/6] fix: codespell fix --- Bit-Manipulation/GenerateSubSets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bit-Manipulation/GenerateSubSets.js b/Bit-Manipulation/GenerateSubSets.js index 70d2022a67..0877e70752 100644 --- a/Bit-Manipulation/GenerateSubSets.js +++ b/Bit-Manipulation/GenerateSubSets.js @@ -5,7 +5,7 @@ * @example [1,2] -> [[],[1],[2],[1,2]] */ -// The time complexity of thhis algorithm is BigO(2^n) where n is the length of array +// The time complexity of this algorithm is BigO(2^n) where n is the length of array function generateSubSets(inputArray) { if (!Array.isArray(inputArray)) { return 'Input is not an array' From cac6f14c44277cbd14673113396b142bbede11ee Mon Sep 17 00:00:00 2001 From: Nikunj Bisht Date: Fri, 13 Oct 2023 03:51:08 +0530 Subject: [PATCH 5/6] error handled --- Bit-Manipulation/GenerateSubSets.js | 5 ++++- Bit-Manipulation/test/GenerateSubSets.test.js | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Bit-Manipulation/GenerateSubSets.js b/Bit-Manipulation/GenerateSubSets.js index 0877e70752..2542506284 100644 --- a/Bit-Manipulation/GenerateSubSets.js +++ b/Bit-Manipulation/GenerateSubSets.js @@ -8,7 +8,10 @@ // The time complexity of this algorithm is BigO(2^n) where n is the length of array function generateSubSets(inputArray) { if (!Array.isArray(inputArray)) { - return 'Input is not an array' + throw new TypeError('Provided input is not an array') + } + if (inputArray.length > 32) { + throw new RangeError('Error size should be less than equal to 32') } let arrayLength = inputArray.length let subSets = [] diff --git a/Bit-Manipulation/test/GenerateSubSets.test.js b/Bit-Manipulation/test/GenerateSubSets.test.js index 2abf578655..42d6e78c24 100644 --- a/Bit-Manipulation/test/GenerateSubSets.test.js +++ b/Bit-Manipulation/test/GenerateSubSets.test.js @@ -1,3 +1,4 @@ +import { expect } from 'vitest' import { generateSubSets } from '../GenerateSubSets' describe('subSets', () => { From 36dbdfe2a40cda41e348bd8584e09b74f9298ba8 Mon Sep 17 00:00:00 2001 From: Nikunj Bisht Date: Sat, 14 Oct 2023 00:13:25 +0530 Subject: [PATCH 6/6] added test cases for error --- Bit-Manipulation/GenerateSubSets.js | 2 +- Bit-Manipulation/test/GenerateSubSets.test.js | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Bit-Manipulation/GenerateSubSets.js b/Bit-Manipulation/GenerateSubSets.js index 2542506284..9ee131d757 100644 --- a/Bit-Manipulation/GenerateSubSets.js +++ b/Bit-Manipulation/GenerateSubSets.js @@ -8,7 +8,7 @@ // The time complexity of this algorithm is BigO(2^n) where n is the length of array function generateSubSets(inputArray) { if (!Array.isArray(inputArray)) { - throw new TypeError('Provided input is not an array') + throw new Error('Provided input is not an array') } if (inputArray.length > 32) { throw new RangeError('Error size should be less than equal to 32') diff --git a/Bit-Manipulation/test/GenerateSubSets.test.js b/Bit-Manipulation/test/GenerateSubSets.test.js index 42d6e78c24..2e3b90ba71 100644 --- a/Bit-Manipulation/test/GenerateSubSets.test.js +++ b/Bit-Manipulation/test/GenerateSubSets.test.js @@ -1,4 +1,3 @@ -import { expect } from 'vitest' import { generateSubSets } from '../GenerateSubSets' describe('subSets', () => { @@ -14,5 +13,24 @@ describe('subSets', () => { [1, 2, 3] ]) expect(generateSubSets([1, 2])).toEqual([[], [1], [2], [1, 2]]) + expect(generateSubSets([1, 2, 3])).toEqual([ + [], + [1], + [2], + [1, 2], + [3], + [1, 3], + [2, 3], + [1, 2, 3] + ]) + expect(() => generateSubSets('invalid')).toThrow( + 'Provided input is not an array' + ) + expect(() => + generateSubSets([ + 1, 2, 2, 1, 2, 3, 4, 3, 2, 3, 4, 3, 2, 2, 2, 3, 12, 11, 4, 2, 2, 2, 2, + 1, 2, 3, 5, 6, 7, 7, 8, 6, 5, 6, 7, 8, 9, 8, 0, 6 + ]) + ).toThrow('Error size should be less than equal to 32') }) })