diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c33..cb6375ed 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -18,12 +18,10 @@ */ export function calculateSumAndProduct(numbers) { let sum = 0; - for (const num of numbers) { - sum += num; - } - let product = 1; + for (const num of numbers) { + sum += num; product *= num; } diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5d..89c24f4c 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -9,6 +9,13 @@ * @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 set = new Set(secondArray); + + return [... new Set(firstArray.filter(item => set.has(item)))] +} \ No newline at end of file diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f6..dd1b94b3 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -9,13 +9,23 @@ * @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; - } + if (numbers[i] + numbers[i + 1] === target) { + return true } } + return false; -} +} \ No newline at end of file diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f7711..b7756bbe 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -10,25 +10,16 @@ */ export function removeDuplicates(inputSequence) { const uniqueItems = []; + const seen = new Set(); 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]); + if (!seen.has(inputSequence[currentIndex])) { + seen.add(inputSequence[currentIndex]); + uniqueItems.push(inputSequence[currentIndex]) } } 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 cfd5cfdf..9c639734 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 @@ -16,16 +16,20 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: Space Complexity: Optimal time complexity: """ + + """ + In this case, there is no changes in the functionality that can change the time complexity as the complexity is already O(n) and only extra space is used. + + In this case, I can just make the code easier to read and cleaner. + """ # 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: + sum += current_number product *= current_number return {"sum": sum, "product": product} 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 478e2efc..50353e25 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -13,9 +13,16 @@ def find_common_items( 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) + + """ In this case, the time complexity is O(n * m), it will only become O(n^2) when the two arrays have equal lengths. This case easily be avoided by using a set to store the second sequence. When checking for a match in the set using item in second-set, this has a time complexity of O(1). + + """ + + second_set = set(second_sequence) + common_items = [] + + for item in first_sequence: + + if item in second_set: + common_items.append(item) return common_items 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 fe2da517..3eda50ce 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 @@ -11,8 +11,9 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: 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 i in range(len(numbers) - 1): + + if numbers[i] + numbers[i + 1] == target_sum: + return True + return False diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe80..9070c4e7 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -11,15 +11,18 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: Space complexity: Optimal time complexity: """ + + """ + The time complexity in the new implementation is O(n) because we only loop and iterated through the array once. The use of set to add and check value for occurrence is O(1) which is the best Optimal time complexity + """ + unique_items = [] + new_set = set() 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 new_set: unique_items.append(value) + new_set.add(value) return unique_items diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index e69de29b..42f9dfa6 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -0,0 +1,60 @@ +class Node: + __slots__ = ("value", "previous", "next") + + def __init__(self, value): + self.value = value + self.previous = None + self.next = None + + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def push_head(self, value): + node = Node(value) + + if self.head is None: + # empty list + self.head = self.tail = node + else: + node.next = self.head + self.head.previous = node + self.head = node + + return node #This is the handle + + def pop_tail(self): + if self.tail is None: + raise IndexError("Pop from empty list") + + node = self.tail + + if node.previous is None: + # One element + self.head = self.tail = None + else: + self.tail = node.previous + self.tail.next = None + + return node.value + + def remove(self, node): + if node.previous: + node.previous.next = node.next + + else: + # removing head + self.head = node.next + + if node.next: + node.next.previous = node.previous + else: + # removing the tail + self.tail = node.previous + + # Clean up references + + node.previous = None + node.next = None \ No newline at end of file