From 4e38ab5331b6cf6f53c0477563ee4a8318ac02b8 Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Sat, 30 Sep 2023 14:40:27 +0530 Subject: [PATCH 1/8] fix: #758 optimised armstrongNumber code --- Maths/ArmstrongNumber.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Maths/ArmstrongNumber.js b/Maths/ArmstrongNumber.js index a3ab78299f..8f861d8c15 100644 --- a/Maths/ArmstrongNumber.js +++ b/Maths/ArmstrongNumber.js @@ -9,16 +9,13 @@ */ const armstrongNumber = (num) => { - if (num < 0 || typeof num !== 'number') return false - - let newSum = 0 - - const numArr = num.toString().split('') - numArr.forEach((num) => { - newSum += parseInt(num) ** numArr.length - }) - - return newSum === num + if (typeof num !== 'number' || num < 0) return false + const numStr = num.toString() + const sum = [...numStr].reduce( + (acc, digit) => acc + parseInt(digit) ** numStr.length, + 0 + ) + return sum === num } export { armstrongNumber } From 184ae84413d01387f6808cbab9a2c1a6052aa46e Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Sun, 1 Oct 2023 06:03:35 +0530 Subject: [PATCH 2/8] fix:#758 Average Median code optimised --- Maths/AverageMedian.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Maths/AverageMedian.js b/Maths/AverageMedian.js index 3be6c551fc..5a6bb26c56 100644 --- a/Maths/AverageMedian.js +++ b/Maths/AverageMedian.js @@ -9,22 +9,14 @@ */ const averageMedian = (sourceArrayOfNumbers) => { - let numbers = [...sourceArrayOfNumbers] - let median = 0 + const numbers = [...sourceArrayOfNumbers].sort(sortNumbers) const numLength = numbers.length - numbers = numbers.sort(sortNumbers) if (numLength % 2 === 0) { - median = (numbers[numLength / 2 - 1] + numbers[numLength / 2]) / 2 - } else { - median = numbers[(numLength - 1) / 2] - } - - return median + return (numbers[numLength / 2 - 1] + numbers[numLength / 2]) / 2 + } else return numbers[Math.floor(numLength / 2)] } -const sortNumbers = (num1, num2) => { - return num1 - num2 -} +const sortNumbers = (num1, num2) => num1 - num2 export { averageMedian } From b8be2f934f883dd0d923d044612145ca698f1c72 Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Sun, 1 Oct 2023 18:03:53 +0530 Subject: [PATCH 3/8] feat: TwoSum function added with test cases --- Maths/TwoSum.js | 23 +++++++++++++++++++++++ Maths/test/TwoSum.test.js | 18 ++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 Maths/TwoSum.js create mode 100644 Maths/test/TwoSum.test.js diff --git a/Maths/TwoSum.js b/Maths/TwoSum.js new file mode 100644 index 0000000000..9b763a60f2 --- /dev/null +++ b/Maths/TwoSum.js @@ -0,0 +1,23 @@ +/* +Given an array of integers, return indices of the two numbers such that they add up to +a specific target. + +You may assume that each input would have exactly one solution, and you may not use the +same element twice. + +Example +Given nums = [2, 7, 11, 15], target = 9, +Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. +return [0, 1]. +*/ + +const TwoSum = (nums, target) => { + const numIndicesMap = {} + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i] + if (complement in numIndicesMap) return [numIndicesMap[complement], i] + numIndicesMap[nums[i]] = i + } + return [] +} +export { TwoSum } diff --git a/Maths/test/TwoSum.test.js b/Maths/test/TwoSum.test.js new file mode 100644 index 0000000000..5d73e24c38 --- /dev/null +++ b/Maths/test/TwoSum.test.js @@ -0,0 +1,18 @@ +import { TwoSum } from '../TwoSum.js' + +describe('Two Sum', () => { + it('Should find the indices of two numbers that add up to the target', () => { + expect(TwoSum([2, 7, 11, 15], 9)).toEqual([0, 1]) + expect(TwoSum([15, 2, 11, 7], 13)).toEqual([1, 2]) + expect(TwoSum([2, 7, 11, 15], 17)).toEqual([0, 3]) + expect(TwoSum([7, 15, 11, 2], 18)).toEqual([0, 2]) + expect(TwoSum([2, 7, 11, 15], 26)).toEqual([2, 3]) + expect(TwoSum([2, 7, 11, 15], 8)).toEqual([]) + expect( + TwoSum( + [...Array(10).keys()].map((i) => 3 * i), + 19 + ) + ).toEqual([]) + }) +}) From 882db433379bd3e57a0240291deedcc93e44469b Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Sun, 1 Oct 2023 18:13:06 +0530 Subject: [PATCH 4/8] revert code --- Maths/TwoSum.js | 23 ----------------------- Maths/test/TwoSum.test.js | 18 ------------------ 2 files changed, 41 deletions(-) delete mode 100644 Maths/TwoSum.js delete mode 100644 Maths/test/TwoSum.test.js diff --git a/Maths/TwoSum.js b/Maths/TwoSum.js deleted file mode 100644 index 9b763a60f2..0000000000 --- a/Maths/TwoSum.js +++ /dev/null @@ -1,23 +0,0 @@ -/* -Given an array of integers, return indices of the two numbers such that they add up to -a specific target. - -You may assume that each input would have exactly one solution, and you may not use the -same element twice. - -Example -Given nums = [2, 7, 11, 15], target = 9, -Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. -return [0, 1]. -*/ - -const TwoSum = (nums, target) => { - const numIndicesMap = {} - for (let i = 0; i < nums.length; i++) { - const complement = target - nums[i] - if (complement in numIndicesMap) return [numIndicesMap[complement], i] - numIndicesMap[nums[i]] = i - } - return [] -} -export { TwoSum } diff --git a/Maths/test/TwoSum.test.js b/Maths/test/TwoSum.test.js deleted file mode 100644 index 5d73e24c38..0000000000 --- a/Maths/test/TwoSum.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import { TwoSum } from '../TwoSum.js' - -describe('Two Sum', () => { - it('Should find the indices of two numbers that add up to the target', () => { - expect(TwoSum([2, 7, 11, 15], 9)).toEqual([0, 1]) - expect(TwoSum([15, 2, 11, 7], 13)).toEqual([1, 2]) - expect(TwoSum([2, 7, 11, 15], 17)).toEqual([0, 3]) - expect(TwoSum([7, 15, 11, 2], 18)).toEqual([0, 2]) - expect(TwoSum([2, 7, 11, 15], 26)).toEqual([2, 3]) - expect(TwoSum([2, 7, 11, 15], 8)).toEqual([]) - expect( - TwoSum( - [...Array(10).keys()].map((i) => 3 * i), - 19 - ) - ).toEqual([]) - }) -}) From c58f28f9f18e0cd5bc628ec0701392eae3e0d4d9 Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Sun, 1 Oct 2023 20:27:33 +0530 Subject: [PATCH 5/8] Fix: #758 used ternary operator to make code more optimised --- Maths/AverageMedian.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Maths/AverageMedian.js b/Maths/AverageMedian.js index 5a6bb26c56..f8f6c64b14 100644 --- a/Maths/AverageMedian.js +++ b/Maths/AverageMedian.js @@ -12,9 +12,9 @@ const averageMedian = (sourceArrayOfNumbers) => { const numbers = [...sourceArrayOfNumbers].sort(sortNumbers) const numLength = numbers.length - if (numLength % 2 === 0) { - return (numbers[numLength / 2 - 1] + numbers[numLength / 2]) / 2 - } else return numbers[Math.floor(numLength / 2)] + return numLength % 2 === 0 + ? (numbers[numLength / 2 - 1] + numbers[numLength / 2]) / 2 + : numbers[Math.floor(numLength / 2)] } const sortNumbers = (num1, num2) => num1 - num2 From bdf8ed8411bbe7fa418e2cfd50bc0250c628eaf8 Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Mon, 2 Oct 2023 06:29:39 +0530 Subject: [PATCH 6/8] Feat: TwoSum function created with test cases --- Maths/test/TwoSum.test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Maths/test/TwoSum.test.js diff --git a/Maths/test/TwoSum.test.js b/Maths/test/TwoSum.test.js new file mode 100644 index 0000000000..5d73e24c38 --- /dev/null +++ b/Maths/test/TwoSum.test.js @@ -0,0 +1,18 @@ +import { TwoSum } from '../TwoSum.js' + +describe('Two Sum', () => { + it('Should find the indices of two numbers that add up to the target', () => { + expect(TwoSum([2, 7, 11, 15], 9)).toEqual([0, 1]) + expect(TwoSum([15, 2, 11, 7], 13)).toEqual([1, 2]) + expect(TwoSum([2, 7, 11, 15], 17)).toEqual([0, 3]) + expect(TwoSum([7, 15, 11, 2], 18)).toEqual([0, 2]) + expect(TwoSum([2, 7, 11, 15], 26)).toEqual([2, 3]) + expect(TwoSum([2, 7, 11, 15], 8)).toEqual([]) + expect( + TwoSum( + [...Array(10).keys()].map((i) => 3 * i), + 19 + ) + ).toEqual([]) + }) +}) From 8bde9b9cd60961962c1ce11f05ee66499ff10868 Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Mon, 2 Oct 2023 06:32:00 +0530 Subject: [PATCH 7/8] Feat: TwoSum function created with test cases --- Maths/TwoSum.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Maths/TwoSum.js diff --git a/Maths/TwoSum.js b/Maths/TwoSum.js new file mode 100644 index 0000000000..9b763a60f2 --- /dev/null +++ b/Maths/TwoSum.js @@ -0,0 +1,23 @@ +/* +Given an array of integers, return indices of the two numbers such that they add up to +a specific target. + +You may assume that each input would have exactly one solution, and you may not use the +same element twice. + +Example +Given nums = [2, 7, 11, 15], target = 9, +Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. +return [0, 1]. +*/ + +const TwoSum = (nums, target) => { + const numIndicesMap = {} + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i] + if (complement in numIndicesMap) return [numIndicesMap[complement], i] + numIndicesMap[nums[i]] = i + } + return [] +} +export { TwoSum } From 5733b78cf66c6fe63b71646cf87ff536b51bf087 Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Tue, 3 Oct 2023 07:05:54 +0530 Subject: [PATCH 8/8] Resolved comments and changes requests --- Maths/TwoSum.js | 31 +++++++++++++++--------------- Maths/test/TwoSum.test.js | 40 ++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/Maths/TwoSum.js b/Maths/TwoSum.js index 9b763a60f2..8aaaea9d14 100644 --- a/Maths/TwoSum.js +++ b/Maths/TwoSum.js @@ -1,22 +1,23 @@ -/* -Given an array of integers, return indices of the two numbers such that they add up to -a specific target. - -You may assume that each input would have exactly one solution, and you may not use the -same element twice. - -Example -Given nums = [2, 7, 11, 15], target = 9, -Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. -return [0, 1]. -*/ +/** + * Given an array of integers, find two numbers that add up to a specific target. + * + * @param {number[]} nums - The array of integers. + * @param {number} target - The target sum. + * @returns {number[]} - An array containing the indices of the two numbers. + * + * @example + * const nums = [2, 7, 11, 15]; + * const target = 9; + * const result = twoSum(nums, target); + * // The function should return [0, 1] because nums[0] + nums[1] = 2 + 7 = 9. + */ const TwoSum = (nums, target) => { - const numIndicesMap = {} + const numIndicesMap = new Map() for (let i = 0; i < nums.length; i++) { const complement = target - nums[i] - if (complement in numIndicesMap) return [numIndicesMap[complement], i] - numIndicesMap[nums[i]] = i + if (numIndicesMap.has(complement)) return [numIndicesMap.get(complement), i] + numIndicesMap.set(nums[i], i) } return [] } diff --git a/Maths/test/TwoSum.test.js b/Maths/test/TwoSum.test.js index 5d73e24c38..851f180a98 100644 --- a/Maths/test/TwoSum.test.js +++ b/Maths/test/TwoSum.test.js @@ -1,18 +1,28 @@ import { TwoSum } from '../TwoSum.js' - describe('Two Sum', () => { - it('Should find the indices of two numbers that add up to the target', () => { - expect(TwoSum([2, 7, 11, 15], 9)).toEqual([0, 1]) - expect(TwoSum([15, 2, 11, 7], 13)).toEqual([1, 2]) - expect(TwoSum([2, 7, 11, 15], 17)).toEqual([0, 3]) - expect(TwoSum([7, 15, 11, 2], 18)).toEqual([0, 2]) - expect(TwoSum([2, 7, 11, 15], 26)).toEqual([2, 3]) - expect(TwoSum([2, 7, 11, 15], 8)).toEqual([]) - expect( - TwoSum( - [...Array(10).keys()].map((i) => 3 * i), - 19 - ) - ).toEqual([]) - }) + const testCasesWithoutSolution = [ + [[8], 8], + [[3, 3, 3, 3], 19] + ] + const testCasesWithSolution = [ + [[2, 7, 11, 15], 9, [0, 1]], + [[15, 2, 11, 7], 13, [1, 2]], + [[2, 7, 11, 15], 17, [0, 3]], + [[7, 15, 11, 2], 18, [0, 2]], + [[2, 7, 11, 15], 26, [2, 3]] + ] + + test.each(testCasesWithoutSolution)( + 'Should return an empty array if there is no solution', + (nums, target) => { + expect(TwoSum(nums, target)).toEqual([]) + } + ) + + test.each(testCasesWithSolution)( + 'Should return the indices of two numbers that add up to the target', + (nums, target, expected) => { + expect(TwoSum(nums, target)).toEqual(expected) + } + ) })