From e3bc86881bc85e298af776711bdc6f83e6f92563 Mon Sep 17 00:00:00 2001 From: nadika Date: Wed, 18 Jun 2025 16:51:51 +0100 Subject: [PATCH 1/7] Refactor calculateSumAndProduct --- .../calculateSumAndProduct.js | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..70ed0f1 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -17,18 +17,37 @@ * @returns {Object} Object containing running total and product */ export function calculateSumAndProduct(numbers) { - let sum = 0; - for (const num of numbers) { - sum += num; - } + // We went through the list two times, first to add the numbers, then to multiply them. + // But we can do both in one loop. This makes the code a bit simpler and faster. + + // let sum = 0; + // for (const num of numbers) { + // sum += num; + // } - let product = 1; - for (const num of numbers) { - product *= num; - } + // let product = 1; + // for (const num of numbers) { + // product *= num; + // } - return { - sum: sum, - product: product, - }; + // return { + // sum: sum, + // product: product, + // }; + + // My solution + + let sum = 0; + let product = 1; + for (const num of numbers) { + sum += num; + product *= num; + } + + return { + sum: sum, + product: product, + }; } + + From 24fe04b40ea4355f6559addd66c15f7dd3490bcc Mon Sep 17 00:00:00 2001 From: nadika Date: Wed, 18 Jun 2025 17:15:55 +0100 Subject: [PATCH 2/7] Refactor findCommonItems --- .../findCommonItems/findCommonItems.js | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..ecee3ab 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,33 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(m + n) — build set and loop through arrays once + * Space Complexity: O(n) — store second array in a set + * Optimal Time Complexity: O(m + n) * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare * @returns {Array} Array containing unique common items */ -export const findCommonItems = (firstArray, secondArray) => [ - ...new Set(firstArray.filter((item) => secondArray.includes(item))), -]; +// export const findCommonItems = (firstArray, secondArray) => [ +// ...new Set(firstArray.filter((item) => secondArray.includes(item))), +// ]; + +// My solution. Refactored to use a Set for faster lookups, making the code more efficient + +export const findCommonItems = (firstArray, secondArray) => { + // Turn secondArray into a Set to quickly check if items exist + const secondSet = new Set(secondArray); + // Create a Set to keep track of common items without duplicates + const commonItemsSet = new Set(); + + // Go through each item in firstArray + for (const element of firstArray) { + // If the item is found in secondSet, add it to commonItemsSet + if (secondSet.has(element)) { + commonItemsSet.add(element); + } + } + // Change the Set of common items back into an array to return + return [...commonItemsSet]; +}; From 3baf15ff90851ec040230fa62b47cdf27a933edc Mon Sep 17 00:00:00 2001 From: nadika Date: Wed, 18 Jun 2025 20:30:57 +0100 Subject: [PATCH 3/7] Refactor hasPairWithSum function --- .../hasPairWithSum/hasPairWithSum.js | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..61253b0 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,37 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n²) original version used a nested loop to check all pairs + * Space Complexity: O(1) no extra space used in the original version + * Optimal Time Complexity: O(n) — in the refactored version using a Set for lookups * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find * @returns {boolean} True if pair exists, false otherwise */ +// export function hasPairWithSum(numbers, target) { +// for (let i = 0; i < numbers.length; i++) { +// for (let j = i + 1; j < numbers.length; j++) { +// if (numbers[i] + numbers[j] === target) { +// return true; +// } +// } +// } +// return false; +// } + +// My solution - Refactored to use a Set for faster lookup, reducing time from O(n²) to O(n) export function hasPairWithSum(numbers, target) { - for (let i = 0; i < numbers.length; i++) { - for (let j = i + 1; j < numbers.length; j++) { - if (numbers[i] + numbers[j] === target) { - return true; - } + const previousNumbers = new Set(); + + for (const num of numbers) { + const neededValue = target - num; + if (previousNumbers.has(neededValue)) { + return true; + } + previousNumbers.add(num); } - } - return false; + + return false; } + From 24d4956edce2d4c4d4434158ac8105495821e8e5 Mon Sep 17 00:00:00 2001 From: nadika Date: Wed, 18 Jun 2025 21:07:12 +0100 Subject: [PATCH 4/7] Refactor removeDuplicates --- .../removeDuplicates/removeDuplicates.mjs | 63 ++++++++++++------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..14eacf6 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,51 @@ /** * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n²) original, O(n) optimised using Set + * Space Complexity: O(n) for 2 options + * Optimal Time Complexity: O(n) linear time is the best possible * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ +// export function removeDuplicates(inputSequence) { +// const uniqueItems = []; + +// for ( +// let currentIndex = 0; +// currentIndex < inputSequence.length; +// currentIndex++ +// ) { +// let isDuplicate = false; +// for ( +// let compareIndex = 0; +// compareIndex < uniqueItems.length; +// compareIndex++ +// ) { +// if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { +// isDuplicate = true; +// break; +// } +// } +// if (!isDuplicate) { +// uniqueItems.push(inputSequence[currentIndex]); +// } +// } + +// return uniqueItems; +// } + export function removeDuplicates(inputSequence) { - const uniqueItems = []; + const seenItems = new Set(); + const uniqueSequence = []; - for ( - let currentIndex = 0; - currentIndex < inputSequence.length; - currentIndex++ - ) { - let isDuplicate = false; - for ( - let compareIndex = 0; - compareIndex < uniqueItems.length; - compareIndex++ - ) { - if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { - isDuplicate = true; - break; - } + for (const item of inputSequence) { + if (!seenItems.has(item)) { + seenItems.add(item); + uniqueSequence.push(item); + } } - if (!isDuplicate) { - uniqueItems.push(inputSequence[currentIndex]); - } - } - return uniqueItems; + return uniqueSequence; } + From 11e80c9674c894753d257afdbdd567667e7a13a2 Mon Sep 17 00:00:00 2001 From: nadika Date: Wed, 18 Jun 2025 21:11:48 +0100 Subject: [PATCH 5/7] Add comments --- .../calculateSumAndProduct/calculateSumAndProduct.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index 70ed0f1..12e1252 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,9 +9,9 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) original has two passes, optimised uses one pass + * Space Complexity: O(1) for both + * Optimal Time Complexity: O(n) linear time is the best possible * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product From c76f9953222f69d4493a943be56296d809ad969a Mon Sep 17 00:00:00 2001 From: nadika Date: Wed, 18 Jun 2025 21:16:52 +0100 Subject: [PATCH 6/7] Add coments to compare --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 61253b0..623b314 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,8 +1,8 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: O(n²) original version used a nested loop to check all pairs - * Space Complexity: O(1) no extra space used in the original version + * Time Complexity: O(n²) original (nested loops), O(n) refactored (using Set) + * Space Complexity: O(1) original, O(n) refactored (Set) * Optimal Time Complexity: O(n) — in the refactored version using a Set for lookups * * @param {Array} numbers - Array of numbers to search through From 136d0cad03c33d063b01657ebd73a2a4d6b20f21 Mon Sep 17 00:00:00 2001 From: nadika Date: Wed, 18 Jun 2025 21:27:34 +0100 Subject: [PATCH 7/7] Refactor calculate_sum_product in Python --- .../calculate_sum_and_product.py | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py index cfd5cfd..36d20a4 100644 --- a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py +++ b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py @@ -12,20 +12,32 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: "sum": 10, // 2 + 3 + 5 "product": 30 // 2 * 3 * 5 } - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity: O(2n) original (two passes), O(n) optimised (one pass) + Space Complexity: O(1) for both versions + Optimal time complexity: O(n) linear time is the best possible """ + # Edge case: empty list + # if not input_numbers: + # return {"sum": 0, "product": 1} + + # sum = 0 + # for current_number in input_numbers: + # sum += current_number + + # product = 1 + # for current_number in input_numbers: + # product *= current_number + + # return {"sum": sum, "product": product} + # Edge case: empty list if not input_numbers: return {"sum": 0, "product": 1} - sum = 0 - for current_number in input_numbers: - sum += current_number - - product = 1 + total_sum = 0 + total_product = 1 for current_number in input_numbers: - product *= current_number + total_sum += current_number + total_product *= current_number - return {"sum": sum, "product": product} + return {"sum": total_sum, "product": total_product}