From 4e38ab5331b6cf6f53c0477563ee4a8318ac02b8 Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Sat, 30 Sep 2023 14:40:27 +0530 Subject: [PATCH 1/3] 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/3] 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/3] 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([]) + }) +})