From 1aa6397b5ed95485450957d0baa0950aff92418f Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Sat, 21 Feb 2026 22:03:19 +0000 Subject: [PATCH 1/2] javascript-task intersection a built-in method --- .../findCommonItems/findCommonItems.js | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..add32b1 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -9,6 +9,21 @@ * @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 firstArr = new Set(firstArray); + const secondArr = new Set(secondArray); + + // return [...firstArr].filter((arr) => secondArr.has(arr)); + return [...firstArr.intersection(secondArr)]; +}; + +// * https://www.w3schools.com/js/js_set_methods.asp#mark_set_new +// * Time Complexity: O(m+n) +// * Space Complexity: O(m+n) +// * Optimal Time Complexity: O(m+n) +>>>>>>> 4541adf (intersection a built-in method) From 4074bd64f90f690612f24c5d257a632904940afd Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Tue, 10 Feb 2026 10:51:07 +0000 Subject: [PATCH 2/2] Python tasks --- .../calculate_sum_and_product.py | 64 ++++++++++++------- .../find_common_items/find_common_items.py | 43 +++++++++---- .../has_pair_with_sum/has_pair_with_sum.py | 35 +++++++--- .../remove_duplicates/remove_duplicates.py | 45 ++++++++----- 4 files changed, 129 insertions(+), 58 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..3d47008 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 @@ -1,31 +1,51 @@ from typing import Dict, List -def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: - """ - Calculate the sum and product of integers in a list. - - Note: the sum is every number added together - and the product is every number multiplied together - so for example: [2, 3, 5] would return - { - "sum": 10, // 2 + 3 + 5 - "product": 30 // 2 * 3 * 5 - } - Time Complexity: - Space Complexity: - Optimal time complexity: - """ - # Edge case: empty list +# def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: +# """ +# Calculate the sum and product of integers in a list. + +# Note: the sum is every number added together +# and the product is every number multiplied together +# so for example: [2, 3, 5] would return +# { +# "sum": 10, // 2 + 3 + 5 +# "product": 30 // 2 * 3 * 5 +# } +# Time Complexity: +# Space Complexity: +# Optimal time complexity: +# """ +# # 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} + +def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str,int]: 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 + + for num in input_numbers: + sum += num + product *= num return {"sum": sum, "product": product} + +# """ +# Time Complexity: O(n) +# Space Complexity: O(1) +# Optimal time complexity: O(n) +# """ + 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..2207361 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -3,19 +3,40 @@ ItemType = TypeVar("ItemType") +# def find_common_items( +# first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType] +# ) -> List[ItemType]: +# """ +# Find common items between two arrays. + +# Time Complexity: +# Space Complexity: +# Optimal time complexity: +# """ +# 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 + + def find_common_items( - first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType] -) -> List[ItemType]: + first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType]) -> List[ItemType]: """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity: O(n+m) + Space Complexity: O(m+k) + Optimal time complexity: O(n+m) """ - 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 + set_1 = set(first_sequence) + set_2 = [] + + for seq in set_1: + if seq in second_sequence: + set_2.append(seq) + return set_2 + + + 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..6029298 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 @@ -3,16 +3,31 @@ Number = TypeVar("Number", int, float) +# 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: +# 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 + 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. + set_1 = set(numbers) + result = [] - Time Complexity: - Space Complexity: - 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 + for num in numbers: + differ = target_sum - num + if differ in set_1: + return True + result.append(differ) return False + +# Time Complexity: O(n) +# # Space Complexity: O(n) +# # Optimal time complexity: O(n) \ 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..857d6ee 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -1,25 +1,40 @@ from typing import List, Sequence, TypeVar +from collections import OrderedDict ItemType = TypeVar("ItemType") +# 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: +# """ +# 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 + 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) + Space complexity: O(n) + Optimal time complexity: O(n) """ - 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 + return list(OrderedDict.fromkeys(values)) + +# Used OrderedDict to create a dictionary with the list elements as keys (preserves order) +# Then, convert it back to a list to remove duplicates +# https://www.w3resource.com/python-exercises/list-advanced/python-list-advanced-exercise-8.php#:~:text=The%20Python%20OrderedDict.,order%20of%20the%20remaining%20elements.