From 0b439f0c16dba05068b9cce6f7dd7d6290165b0a Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Mon, 8 Jun 2026 15:30:24 +0100 Subject: [PATCH 01/13] trying to answer calculateSumAndProduct --- .../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 ce738c3..f9ed0f2 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) + * Space Complexity: O(1) + * Optimal Time Complexity: O(N) * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product From f68fe2b3ce319e6a9c8b4b4558b6d9a7485bcff4 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Mon, 8 Jun 2026 17:18:29 +0100 Subject: [PATCH 02/13] completed calculateSumAndProduct exercise --- .../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 f9ed0f2..3094824 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: O(N) - * Space Complexity: O(1) - * Optimal Time Complexity: O(N) + * Time Complexity: O(N), as the function iterates the array twice (2N) + * Space Complexity: O(1), the space for variables remains constant + * Optimal Time Complexity: O(N), the function can't be refactored to reduce complexity, (N) is regarded as the same as (2N) * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product From 4bc73ae48b4b9f0fd357798641d5c31af29b6f64 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Mon, 8 Jun 2026 17:21:12 +0100 Subject: [PATCH 03/13] completed calculate_sum_and_product exercise --- .../calculate_sum_and_product/calculate_sum_and_product.py | 6 +++--- 1 file changed, 3 insertions(+), 3 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..196e80e 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,9 +12,9 @@ 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(N), the function loops the array for twice (2N) + Space Complexity: O(1), the space for variables remains constant + Optimal time complexity: O(N), (N) is regarded as the same as (2N) """ # Edge case: empty list if not input_numbers: From 7c13d125d6fafa711298787e10ecda03aa07512a Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Mon, 8 Jun 2026 18:06:15 +0100 Subject: [PATCH 04/13] completed findCommonItem.js --- .../findCommonItems/findCommonItems.js | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..639daeb 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,22 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(N*M), for each item of loop in the firstArray, it has to loop through the secondArray in the worst case + * Space Complexity: O(N), the worst case is the length of the firstArray + * Optimal Time Complexity: O(N + M), N is for the Set of firstArray, and M is the loop for secondArray. O(N+M) is better than O(N*M). * * @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))), +// ]; + +export const findCommonItems = (firstArray, secondArray) => { + const firstSet = new Set(firstArray); + + const commonInSecond = secondArray.filter((item) => firstSet.has(item)); + + return [... new Set(commonInSecond)]; +} \ No newline at end of file From 367f8d329944430179887fee9bfc430025134f35 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Mon, 8 Jun 2026 18:42:32 +0100 Subject: [PATCH 05/13] completed find_common_items.py --- .../find_common_items/find_common_items.py | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Sprint-1/Python/find_common_items/find_common_items.py b/Sprint-1/Python/find_common_items/find_common_items.py index 478e2ef..f2cf9e4 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -9,13 +9,19 @@ def find_common_items( """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity: O(N*M), it is a loop inside a loop + Space Complexity: O(N), the size of the common_items would grow to N in the worst case + Optimal time complexity: O(N + M), N is for the firstSet, M is the filter in second """ - common_items: List[ItemType] = [] - for i in first_sequence: - for j in second_sequence: - if i == j and i not in common_items: - common_items.append(i) - return common_items + # common_items: List[ItemType] = [] + # for i in first_sequence: + # for j in second_sequence: + # if i == j and i not in common_items: + # common_items.append(i) + # return common_items + + firstSet = {*first_sequence} + + commonInSecond = filter(lambda item: item in firstSet, second_sequence) + + return list({*commonInSecond}) \ No newline at end of file From fafab38dd9ae2b083f00d22f9173c0ade7b1b0e6 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Mon, 8 Jun 2026 21:53:44 +0100 Subject: [PATCH 06/13] completed hasPairWithSum.js --- .../hasPairWithSum/hasPairWithSum.js | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..d27627c 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,34 @@ /** * 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^2), the loop is inside a loop + * Space Complexity: O(1), the array remains the same + * Optimal Time Complexity: O(N), it loops once after optimization * * @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; +// } + 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 targetSet = new Set(); + for (let num of numbers) { + const targetNum = target - num; + if (targetSet.has(targetNum)) { + return true; + } else { + targetSet.add(num) } } return false; -} +} \ No newline at end of file From b1a0f5f1f24aac133ad10b4d6d3f7a015bcc9b62 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Mon, 8 Jun 2026 23:22:34 +0100 Subject: [PATCH 07/13] completed hasPairWithSum --- .../hasPairWithSum/hasPairWithSum.js | 2 +- .../has_pair_with_sum/has_pair_with_sum.py | 24 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index d27627c..98d6951 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -2,7 +2,7 @@ * Find if there is a pair of numbers that sum to a given target value. * * Time Complexity: O(N^2), the loop is inside a loop - * Space Complexity: O(1), the array remains the same + * Space Complexity: O(1) + O(N) = O(N). O(i) is for i and j index counters. O(N) is for the growing targetSet. * Optimal Time Complexity: O(N), it loops once after optimization * * @param {Array} numbers - Array of numbers to search through diff --git a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py index fe2da51..7c11228 100644 --- a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py +++ b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py @@ -7,12 +7,22 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: """ Find if there is a pair of numbers that sum to a target value. - Time Complexity: - Space Complexity: + Time Complexity: O(N^2), a loop inside a loop + Space Complexity: O(1) + O(N) = O(N). O(1) is for the i and j loop counter. O(N) is for the growing targetSet(). Optimal time complexity: """ - for i in range(len(numbers)): - for j in range(i + 1, len(numbers)): - if numbers[i] + numbers[j] == target_sum: - return True - return False + # for i in range(len(numbers)): + # for j in range(i + 1, len(numbers)): + # if numbers[i] + numbers[j] == target_sum: + # return True + # return False + + targetSet = set() + + for num in numbers: + targetNum = target_sum - num + if targetNum in targetSet: + return True + else: + targetSet.add(num) + return False \ No newline at end of file From 0d67520c20d0ae636e6b231c7a04b473aa20fa0a Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Tue, 9 Jun 2026 10:21:28 +0100 Subject: [PATCH 08/13] updated comments of hasPairWithSum --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 4 ++-- Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 98d6951..97cf2bc 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -2,8 +2,8 @@ * Find if there is a pair of numbers that sum to a given target value. * * Time Complexity: O(N^2), the loop is inside a loop - * Space Complexity: O(1) + O(N) = O(N). O(i) is for i and j index counters. O(N) is for the growing targetSet. - * Optimal Time Complexity: O(N), it loops once after optimization + * Space Complexity: O(1). O(i) is for i and j index counters. + * Optimal Time Complexity: O(N), it loops once after optimization. However, the space complexity becomes O(N) as there is a growing targetSet. * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find diff --git a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py index 7c11228..56d83b0 100644 --- a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py +++ b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py @@ -8,8 +8,8 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: Find if there is a pair of numbers that sum to a target value. Time Complexity: O(N^2), a loop inside a loop - Space Complexity: O(1) + O(N) = O(N). O(1) is for the i and j loop counter. O(N) is for the growing targetSet(). - Optimal time complexity: + Space Complexity: O(1). O(1) is for the i and j loop counter. + Optimal time complexity: O(N) as it only loops once, with a trade off space becomes O(N). """ # for i in range(len(numbers)): # for j in range(i + 1, len(numbers)): From 55460569240c67c45c74fceddc3abee771ae5e72 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Tue, 9 Jun 2026 13:43:14 +0100 Subject: [PATCH 09/13] completed removeDuplicates --- .../removeDuplicates/removeDuplicates.mjs | 58 ++++++++++--------- .../remove_duplicates/remove_duplicates.py | 31 ++++++---- 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..18dfd27 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,40 @@ /** * 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^2). It takes a loop inside a loop to compare duplicates. + * Space Complexity: O(N). The uniqueItems array would grow to N in the worst case. + * Optimal Time Complexity: O(N). It only loops once after optimization. * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ -export function removeDuplicates(inputSequence) { - const uniqueItems = []; +// 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]); +// } +// } - 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; +// } - return uniqueItems; -} +export function removeDuplicates(inputSequence) { + return [...new Set(inputSequence)]; +} \ No newline at end of file diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe8..7f73b9d 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -7,19 +7,28 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: """ 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^2). It takes a loop inside a loop + Space complexity: O(N). The space of unique_items would grow to N in the worst case. + Optimal time complexity: O(N). It only loops once after optimization, but the space becomes O(N + N) """ - unique_items = [] + # unique_items = [] + + # for value in values: + # is_duplicate = False + # for existing in unique_items: + # if value == existing: + # is_duplicate = True + # break + # if not is_duplicate: + # unique_items.append(value) + + # return unique_items + seen = set() + unique_items = [] for value in values: - is_duplicate = False - for existing in unique_items: - if value == existing: - is_duplicate = True - break - if not is_duplicate: + if value not in seen: + seen.add(value) unique_items.append(value) - + return unique_items From 489b6f9c66fe7883842c10c5fdab2d1635d81234 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Wed, 10 Jun 2026 12:20:13 +0100 Subject: [PATCH 10/13] updated variable names in has_pair_with_sum.py --- Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py index 56d83b0..51a17c7 100644 --- a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py +++ b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py @@ -17,12 +17,12 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: # return True # return False - targetSet = set() + pair_num_set = set() for num in numbers: - targetNum = target_sum - num - if targetNum in targetSet: + pair_num = target_sum - num + if pair_num in pair_num_set: return True else: - targetSet.add(num) - return False \ No newline at end of file + pair_num_set.add(num) + return False From 1eeda260e05b6c304d93ffbae7c307ca334e9bce Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Wed, 10 Jun 2026 12:33:38 +0100 Subject: [PATCH 11/13] updated to use built-in set in find_common_items.py --- Sprint-1/Python/find_common_items/find_common_items.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-1/Python/find_common_items/find_common_items.py b/Sprint-1/Python/find_common_items/find_common_items.py index f2cf9e4..90a128d 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -20,8 +20,9 @@ def find_common_items( # common_items.append(i) # return common_items - firstSet = {*first_sequence} + first_set = set(first_sequence) - commonInSecond = filter(lambda item: item in firstSet, second_sequence) + common_in_second = filter(lambda item: item in first_set, second_sequence) + + return list(set(common_in_second)) - return list({*commonInSecond}) \ No newline at end of file From 9cc4fdd9eea51a4227fc91edfe3b71cfe4b004b4 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Wed, 10 Jun 2026 14:01:43 +0100 Subject: [PATCH 12/13] updated to one line in find_common_items.py --- Sprint-1/Python/find_common_items/find_common_items.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sprint-1/Python/find_common_items/find_common_items.py b/Sprint-1/Python/find_common_items/find_common_items.py index 90a128d..1db1c8c 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -20,9 +20,5 @@ def find_common_items( # common_items.append(i) # return common_items - first_set = set(first_sequence) - - common_in_second = filter(lambda item: item in first_set, second_sequence) - - return list(set(common_in_second)) + return list(set(first_sequence).intersection(second_sequence)) From 58f4947638a8dd77413a7ecf3c8a28a691415300 Mon Sep 17 00:00:00 2001 From: TzeMingHo Date: Wed, 10 Jun 2026 14:09:48 +0100 Subject: [PATCH 13/13] updated to one line in remove_duplicates.py --- Sprint-1/Python/remove_duplicates/remove_duplicates.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index 7f73b9d..f7f1296 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -23,12 +23,5 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: # unique_items.append(value) # return unique_items - - seen = set() - unique_items = [] - for value in values: - if value not in seen: - seen.add(value) - unique_items.append(value) - return unique_items + return list(dict.fromkeys(values))