From dc06d9502a45f4f9aaa973a91694e9a8a39d3718 Mon Sep 17 00:00:00 2001 From: Marcus Friberg Date: Sat, 18 Feb 2023 16:37:55 +0100 Subject: [PATCH 1/2] fix: fixed error in the MaxProductOfThree algorithm Fixed the error in the MaxProductOfThree by initializing the max and min variables to null instead of -1. The checks were then altered to check for null instead of -1. Also wrote more tests, which randomly generated small arrays and compared the output of the maxProductOfThree-algorithm to the output of a slower, but complete, function which calculates all posible triple-products of the values of the array. Fixes: #1294 --- Dynamic-Programming/MaxProductOfThree.js | 14 ++--- .../tests/MaxProductOfThree.test.js | 53 +++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/Dynamic-Programming/MaxProductOfThree.js b/Dynamic-Programming/MaxProductOfThree.js index 58e5e63628..0f1e0ffcb3 100644 --- a/Dynamic-Programming/MaxProductOfThree.js +++ b/Dynamic-Programming/MaxProductOfThree.js @@ -10,29 +10,29 @@ export function maxProductOfThree (arrayItems) { const n = arrayItems.length if (n < 3) throw new Error('Triplet cannot exist with the given array') let max1 = arrayItems[0] - let max2 = -1 - let max3 = -1 + let max2 = null + let max3 = null let min1 = arrayItems[0] - let min2 = -1 + let min2 = null for (let i = 1; i < n; i++) { if (arrayItems[i] > max1) { max3 = max2 max2 = max1 max1 = arrayItems[i] - } else if (max2 === -1 || arrayItems[i] > max2) { + } else if (max2 === null || arrayItems[i] > max2) { max3 = max2 max2 = arrayItems[i] - } else if (max3 === -1 || arrayItems[i] > max3) { + } else if (max3 === null || arrayItems[i] > max3) { max3 = arrayItems[i] } if (arrayItems[i] < min1) { min2 = min1 min1 = arrayItems[i] - } else if (min2 === -1 || arrayItems[i] < min2) { + } else if (min2 === null || arrayItems[i] < min2) { min2 = arrayItems[i] } } const prod1 = max1 * max2 * max3 const prod2 = max1 * min1 * min2 return Math.max(prod1, prod2) -} +} \ No newline at end of file diff --git a/Dynamic-Programming/tests/MaxProductOfThree.test.js b/Dynamic-Programming/tests/MaxProductOfThree.test.js index 32aaec5427..506231a48a 100644 --- a/Dynamic-Programming/tests/MaxProductOfThree.test.js +++ b/Dynamic-Programming/tests/MaxProductOfThree.test.js @@ -15,3 +15,56 @@ describe('MaxProductOfThree', () => { expect(maxProductOfThree([10, -6, 5, 3, 1, -10])).toBe(600) }) }) + +// Tests using random arrays of size 3 to 5, with values rangin from -4 to 4 +// The output is compared to a slower function that calculates all possible products of 3 numbers in the array and returns the largest one +describe('MaxProductOfThree, random arrays of size 3 to 5', () => { + // Slower function that operates in O(n^3), where n is the length of the input array. + // Calculates all possible products of 3 numbers in the array and returns the largest + function completeMaxThree (array) { + let maximumProduct = null + for (let i = 0; i < array.length - 2; i++) { + for (let j = i + 1; j < array.length - 1; j++) { + for (let k = j + 1; k < array.length; k++) { + const currentProduct = array[i] * array[j] * array[k] + if (maximumProduct === null || currentProduct > maximumProduct) { + maximumProduct = currentProduct + } + } + } + } + return maximumProduct + } + + // Set up consts for the tests + const maxValue = 4 + const minValue = -4 + const maxLength = 5 + const minLength = 3 + const numberOfRandomTests = 5000 + + // Run each test + for (let i = 0; i < numberOfRandomTests; i++) { + const arr = [] + // Randomize the length of the array in the current test + const length = Math.floor(Math.random() * (maxLength - minLength) + minLength) + + // Fill the array with random values in the specified range + for (let j = 0; j < length + 1; j++) { + arr.push(Math.floor(Math.random() * (maxValue - minValue) + minValue)) + } + + // Calculate the actual max product, slow but completely + const expectedProduct = completeMaxThree(arr) + + // Set up the expectation + it('Expect the array ' + arr.toString() + ' to return the maximum three product of ' + expectedProduct, () => { + // Calculate the max three product using the function being tested + const actualProduct = maxProductOfThree(arr) + + // Was unable to use expect().toBe(), since it sometimes compared 0 to -0, and that would not pass + // At the same time, standardjs forbid me from checking for === -0 to convert to 0 + expect(actualProduct === expectedProduct).toBeTruthy() + }) + } +}) \ No newline at end of file From 9e58468b906bba85288bdfbc6c072c202f28e6f6 Mon Sep 17 00:00:00 2001 From: Marcus Friberg Date: Sat, 18 Feb 2023 16:47:04 +0100 Subject: [PATCH 2/2] fix: Added newlines at the end of the files --- Dynamic-Programming/MaxProductOfThree.js | 2 +- Dynamic-Programming/tests/MaxProductOfThree.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dynamic-Programming/MaxProductOfThree.js b/Dynamic-Programming/MaxProductOfThree.js index 0f1e0ffcb3..635aa8ee1d 100644 --- a/Dynamic-Programming/MaxProductOfThree.js +++ b/Dynamic-Programming/MaxProductOfThree.js @@ -35,4 +35,4 @@ export function maxProductOfThree (arrayItems) { const prod1 = max1 * max2 * max3 const prod2 = max1 * min1 * min2 return Math.max(prod1, prod2) -} \ No newline at end of file +} diff --git a/Dynamic-Programming/tests/MaxProductOfThree.test.js b/Dynamic-Programming/tests/MaxProductOfThree.test.js index 506231a48a..68ed8bedd0 100644 --- a/Dynamic-Programming/tests/MaxProductOfThree.test.js +++ b/Dynamic-Programming/tests/MaxProductOfThree.test.js @@ -67,4 +67,4 @@ describe('MaxProductOfThree, random arrays of size 3 to 5', () => { expect(actualProduct === expectedProduct).toBeTruthy() }) } -}) \ No newline at end of file +}) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy