Skip to content

London | Nadika Zavodovska | Module-Complexity | Sprint 1 | Analyse and refactor #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,45 @@
* "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<number>} 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;
}
// 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,
};
}


31 changes: 25 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -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];
};
36 changes: 26 additions & 10 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -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 (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<number>} 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;
}

63 changes: 39 additions & 24 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -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;
}

Original file line number Diff line number Diff line change
Expand Up @@ -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}