From 83cb4b144ccbc45dfc459b92b383da36adceaa85 Mon Sep 17 00:00:00 2001 From: syr4300 <110762582+syr4300@users.noreply.github.com> Date: Fri, 2 Sep 2022 17:43:39 +0800 Subject: [PATCH 1/6] Create SelectMedian.js --- .../math/SelectMedian/SelectMedian.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/algorithms/math/SelectMedian/SelectMedian.js diff --git a/src/algorithms/math/SelectMedian/SelectMedian.js b/src/algorithms/math/SelectMedian/SelectMedian.js new file mode 100644 index 0000000000..6de324b549 --- /dev/null +++ b/src/algorithms/math/SelectMedian/SelectMedian.js @@ -0,0 +1,29 @@ +/** + * @function selectMedian + * @description This algorithm will find the median of the array + * + * @param {Integer[]} + * + * @return {median} + */ + +const selectMedian = (sourceArrayOfNumbers) => { + let numbers = [...sourceArrayOfNumbers] + let median = 0 + 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 +} + +const sortNumbers = (num1, num2) => { + return num1 - num2 +} + +export { selectMedian } From f857749e6d2d4a400ac252c0207d1e47f01c4c02 Mon Sep 17 00:00:00 2001 From: syr4300 <110762582+syr4300@users.noreply.github.com> Date: Fri, 2 Sep 2022 17:45:49 +0800 Subject: [PATCH 2/6] Create SelectMedian.test.js --- .../__test__/SelectMedian.test.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/algorithms/math/SelectMedian/__test__/SelectMedian.test.js diff --git a/src/algorithms/math/SelectMedian/__test__/SelectMedian.test.js b/src/algorithms/math/SelectMedian/__test__/SelectMedian.test.js new file mode 100644 index 0000000000..53a54aaf4d --- /dev/null +++ b/src/algorithms/math/SelectMedian/__test__/SelectMedian.test.js @@ -0,0 +1,21 @@ +import { selectMedian } from '../SelectMedian' + +test('should return the median of an array of numbers:', () => { + const medianValue = selectMedian([1, 3, 6, 4, 5]) + expect(medianValue).toBe(4) +}) + +test('should return the median of an array of numbers:', () => { + const medianValue = selectMedian([8, 9, 1, 3, 5, 10, 11]) + expect(medianValue).toBe(8) +}) + +test('should return the median of an array of numbers:', () => { + const medianValue = selectMedian([15, 18, 3, 9, 13, 5]) + expect(medianValue).toBe(11) +}) + +test('should return the median of an array of numbers:', () => { + const medianValue = selectMedian([1, 3, 3, 4, 6, 8]) + expect(medianValue).toBe(3.5) +}) From ec04bd8a587185afe16b1b3414c6dc5f4e5a74c1 Mon Sep 17 00:00:00 2001 From: syr4300 <110762582+syr4300@users.noreply.github.com> Date: Fri, 2 Sep 2022 17:46:42 +0800 Subject: [PATCH 3/6] Create MaxRecursion.js --- .../math/MaxRecursion/MaxRecursion.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/algorithms/math/MaxRecursion/MaxRecursion.js diff --git a/src/algorithms/math/MaxRecursion/MaxRecursion.js b/src/algorithms/math/MaxRecursion/MaxRecursion.js new file mode 100644 index 0000000000..c07179ef24 --- /dev/null +++ b/src/algorithms/math/MaxRecursion/MaxRecursion.js @@ -0,0 +1,30 @@ + /* + * @function maxRecursion + * @description This algorithm will find the maximum value of a array of numbers. + * + * @param {Integer[]} arr Array of numbers + * @param {Integer} left Index of the first element + * @param {Integer} right Index of the last element + * + * @return {Integer} Maximum value of the array + */ + +function maxRecursion (arr, left, right) { + const len = arr.length + if (len === 0 || !arr) { + return undefined + } + if (left >= len || left < -len || right >= len || right < -len) { + throw new Error('Index out of range') + } + if (left === right) { + return arr[left] + } + // n >> m is equivalent to floor(n / pow(2, m)), floor(n / 2) in this case, which is the mid index + const mid = (left + right) >> 1 + const leftMax = maxRecursion(arr, left, mid) + const rightMax = maxRecursion(arr, mid + 1, right) + // Return the maximum + return Math.max(leftMax, rightMax) +} +export { maxRecursion } From a8c990646b1b36c0a3bc2ef22ec2e01f3624948c Mon Sep 17 00:00:00 2001 From: syr4300 <110762582+syr4300@users.noreply.github.com> Date: Fri, 2 Sep 2022 17:47:15 +0800 Subject: [PATCH 4/6] Create MaxRecursion.test.js --- .../__test__/MaxRecursion.test.js | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/algorithms/math/MaxRecursion/__test__/MaxRecursion.test.js diff --git a/src/algorithms/math/MaxRecursion/__test__/MaxRecursion.test.js b/src/algorithms/math/MaxRecursion/__test__/MaxRecursion.test.js new file mode 100644 index 0000000000..0a047e0ae0 --- /dev/null +++ b/src/algorithms/math/MaxRecursion/__test__/MaxRecursion.test.js @@ -0,0 +1,39 @@ +import { maxRecursion } from '../MaxRecursion' + +describe('Test maxRecursion function', () => { + const positiveArray = [1, 2, 4, 5] + + const negativeArray = [-1, -2, -4, -5] + + const positiveAndNegativeArray = [1, 2, 4, 5, -1, -2, -4, -5] + + const zeroArray = [0, 0, 0, 0] + + const emptyArray = [] + + it('Testing with positive arrays', () => { + expect(maxRecursion(positiveArray, 0, positiveArray.length - 1)).toBe(5) + }) + + it('Testing with negative arrays', () => { + expect(maxRecursion(negativeArray, 0, negativeArray.length - 1)).toBe(-1) + }) + + it('Testing with positive and negative arrays', () => { + expect( + maxRecursion( + positiveAndNegativeArray, + 0, + positiveAndNegativeArray.length - 1 + ) + ).toBe(5) + }) + + it('Testing with zero arrays', () => { + expect(maxRecursion(zeroArray, 0, zeroArray.length - 1)).toBe(0) + }) + + it('Testing with empty arrays', () => { + expect(maxRecursion(emptyArray, 0, emptyArray.length - 1)).toBe(undefined) + }) +}) From a15288c7aae2c61e72ea8e6fc5315262c1c878d0 Mon Sep 17 00:00:00 2001 From: syr4300 <110762582+syr4300@users.noreply.github.com> Date: Fri, 2 Sep 2022 18:01:43 +0800 Subject: [PATCH 5/6] Update src/algorithms/math/MaxRecursion/MaxRecursion.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/math/MaxRecursion/MaxRecursion.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/math/MaxRecursion/MaxRecursion.js b/src/algorithms/math/MaxRecursion/MaxRecursion.js index c07179ef24..c1f474dc19 100644 --- a/src/algorithms/math/MaxRecursion/MaxRecursion.js +++ b/src/algorithms/math/MaxRecursion/MaxRecursion.js @@ -1,4 +1,4 @@ - /* +/* * @function maxRecursion * @description This algorithm will find the maximum value of a array of numbers. * From 58a8381bf4cc3f57155c029340c37795dfa6623b Mon Sep 17 00:00:00 2001 From: syr4300 <110762582+syr4300@users.noreply.github.com> Date: Sat, 3 Sep 2022 18:38:23 +0800 Subject: [PATCH 6/6] Update src/algorithms/math/MaxRecursion/MaxRecursion.js Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> --- src/algorithms/math/MaxRecursion/MaxRecursion.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/math/MaxRecursion/MaxRecursion.js b/src/algorithms/math/MaxRecursion/MaxRecursion.js index c1f474dc19..5110246854 100644 --- a/src/algorithms/math/MaxRecursion/MaxRecursion.js +++ b/src/algorithms/math/MaxRecursion/MaxRecursion.js @@ -9,7 +9,7 @@ * @return {Integer} Maximum value of the array */ -function maxRecursion (arr, left, right) { +function maxRecursion(arr, left, right) { const len = arr.length if (len === 0 || !arr) { return undefined