diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..b2e18c5 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,26 +9,34 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(N) 2 for loops for loops are 0 + * Space Complexity: O(1) 2 variables + * Optimal Time Complexity: O(N) * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product */ export function calculateSumAndProduct(numbers) { - let sum = 0; - for (const num of numbers) { - sum += num; - } + // 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, - }; + // product and sum can be combined to one loop + let sum = 0; + let product = 1; + + for (const num of numbers) { + sum += num; + product *= num; + } + return { + sum: sum, + product: product, + }; } diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..de7c18f 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,32 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: Logarythmic? No Quadratic + * +// nested so includes loops over each item in first arr +// nested loops are not efficient and used filter and inlcudes +//array.inlcudes method goes item by item + * Space Complexity: O(N) + * Optimal Time Complexity: o(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))), + const dictToCheck = {}; + const doubled = []; + + for (const item of firstArray) { + dictToCheck[item] = true; + } + + for (const item of secondArray) { + if (dictToCheck[item]) { + doubled.push(item); + dictToCheck[item] = false; + } + } + return doubled; +}; diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..082b298 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:Quadratic time + * nested loop so inefficient comparing of each i j to target + * Space Complexity: O(N) + * Optimal Time Complexity: O(N) * * @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) { +// we are now making a cake from cake slices +// numbers are now slices of cake and target is a a number of slices taht makes a whole cake + +export function hasPairWithSum(slices, targetWholeCake) { + // 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 inventoryOfSlices = {}; + + for (const slice of slices) { + const missingPieces = targetWholeCake - slice; + + if (inventoryOfSlices[missingPieces]) { + return true; + } + + inventoryOfSlices[slice] = true; // add slice + } + return false; } diff --git a/Sprint-1/JavaScript/package-lock.json b/Sprint-1/JavaScript/package-lock.json new file mode 100644 index 0000000..37ca6f0 --- /dev/null +++ b/Sprint-1/JavaScript/package-lock.json @@ -0,0 +1,12 @@ +{ + "name": "module-complexity-sprint-1", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "module-complexity-sprint-1", + "version": "1.0.0" + } + } +} diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..39c517f 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,44 @@ /** * 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: Quadratic + * nested loop + * Space Complexity: O(N) + * Optimal Time Complexity:O(N) * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ export function removeDuplicates(inputSequence) { - const uniqueItems = []; + 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]); + // } + // } + const itemsWeChecked = {}; + for (const item of inputSequence) { + if (!itemsWeChecked[item]) { + uniqueItems.push(item); + itemsWeChecked[item] = true; + } + } - return uniqueItems; + return uniqueItems; } 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..6ae615c 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,25 @@ 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) + Space Complexity: O(1) + Optimal time complexity:O(N) """ # Edge case: empty list if not input_numbers: return {"sum": 0, "product": 1} - sum = 0 - for current_number in input_numbers: - sum += current_number + # sum = 0 + # for current_number in input_numbers: + # sum += current_number + + # product = 1 + # for current_number in input_numbers: + # product *= current_number + sum = 0 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 478e2ef..6252aa4 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,26 @@ def find_common_items( """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity: Quadratic + Space Complexity: ON) + Optimal time complexity: O(N) """ 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) + # 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 + + dictToCheck = {} + + for item in first_sequence: + dictToCheck[item] = True + + for item in second_sequence: + if item in dictToCheck: + common_items.append(item) + dictToCheck.pop(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 fe2da51..8e9adb9 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,23 @@ 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: + Time Complexity:Quadratic + Space Complexity:O(N) + Optimal time complexity:O(N) """ 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 + # // i am using my cake exqmple from js + inventory_of_slices = {} + + for slice in numbers: + missing_piece = target_sum - slice + if missing_piece in inventory_of_slices: + return True + + inventory_of_slices[slice] = True + + return False \ 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..a16707c 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:quadratic + 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) + # 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 unique_items + + + itemsWeChecked = {} + for item in values: + if item not in itemsWeChecked: + unique_items.append(item) + itemsWeChecked[item] = True + + return unique_items \ No newline at end of file