From 9c07bb110758aae6316385c966b42add948b0586 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Thu, 26 Oct 2023 19:04:20 +0530 Subject: [PATCH 1/3] Test cases project euler 9 (#1571) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📦 NEW: Added solution for ProjectEuler-007 * 🐛 FIX: Spelling mistake fixes * 👌 IMPROVE: changed variable name from `inc` to `candidateValue` and thrown error in case of invalid input * 👌 IMPROVE: Modified the code * 👌 IMPROVE: Added test case for ProjectEuler Problem001 * 👌 IMPROVE: Added test cases for Project Euler Problem 4 * 👌 IMPROVE: auto prettier fixes * 👌 IMPROVE: Added test cases for project euler problem 9 * Updated Documentation in README.md * Updated Documentation in README.md --------- Co-authored-by: Omkarnath Parida Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- Project-Euler/test/Problem009.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Project-Euler/test/Problem009.test.js diff --git a/Project-Euler/test/Problem009.test.js b/Project-Euler/test/Problem009.test.js new file mode 100644 index 0000000000..3919fa2833 --- /dev/null +++ b/Project-Euler/test/Problem009.test.js @@ -0,0 +1,8 @@ +import { findSpecialPythagoreanTriplet } from '../Problem009.js' + +describe('Pythagorean Triplet', () => { + // Project Euler Condition Check + test('the multiplication of the pythagorean triplet where a + b + c = 1000', () => { + expect(findSpecialPythagoreanTriplet()).toBe(31875000) + }) +}) From f67cdc3cad62924de6c29083b7d2776f90655cbe Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Thu, 26 Oct 2023 19:05:34 +0530 Subject: [PATCH 2/3] Test cases project euler 6 (#1570) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📦 NEW: Added solution for ProjectEuler-007 * 🐛 FIX: Spelling mistake fixes * 👌 IMPROVE: changed variable name from `inc` to `candidateValue` and thrown error in case of invalid input * 👌 IMPROVE: Modified the code * 👌 IMPROVE: Added test case for ProjectEuler Problem001 * 👌 IMPROVE: Added test cases for Project Euler Problem 4 * 👌 IMPROVE: auto prettier fixes * 👌 IMPROVE: added test cases for project euler problem 6 * Updated Documentation in README.md * Updated Documentation in README.md --------- Co-authored-by: Omkarnath Parida Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- Project-Euler/test/Problem006.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Project-Euler/test/Problem006.test.js diff --git a/Project-Euler/test/Problem006.test.js b/Project-Euler/test/Problem006.test.js new file mode 100644 index 0000000000..1323a34ac2 --- /dev/null +++ b/Project-Euler/test/Problem006.test.js @@ -0,0 +1,11 @@ +import { squareDifference } from '../Problem006.js' + +describe('Square Difference', () => { + test('difference between the sum of the squares of the first ten natural numbers and the square of the sum', () => { + expect(squareDifference(10)).toBe(2640) + }) + // Project Euler Condition Check + test('difference between the sum of the squares of the first one hundred natural numbers and the square of the sum', () => { + expect(squareDifference()).toBe(25164150) + }) +}) From d671327ebe63832434285ef96cabf255e11bf2cd Mon Sep 17 00:00:00 2001 From: Nikunj Bisht <52692588+Nikunj-bisht@users.noreply.github.com> Date: Thu, 26 Oct 2023 19:08:19 +0530 Subject: [PATCH 3/3] feat: added find subsets algorithm using bitmanipulation (#1514) * feat: added find subsets algorithm using bitmanipulation * file name fix * test file name fix * fix: codespell fix * error handled * added test cases for error --- Bit-Manipulation/GenerateSubSets.js | 34 ++++++++++++++++++ Bit-Manipulation/test/GenerateSubSets.test.js | 36 +++++++++++++++++++ 2 files changed, 70 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..9ee131d757 --- /dev/null +++ b/Bit-Manipulation/GenerateSubSets.js @@ -0,0 +1,34 @@ +/** + * @function generateSubSets + * @param {Array} inputArray + * @returns {Array} + * @example [1,2] -> [[],[1],[2],[1,2]] + */ + +// 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 Error('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 = [] + // 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..2e3b90ba71 --- /dev/null +++ b/Bit-Manipulation/test/GenerateSubSets.test.js @@ -0,0 +1,36 @@ +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]]) + 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') + }) +})