Skip to content

Commit 73b83b2

Browse files
committed
Refactor: optimize the js and py scripts to optimize time and space complexity.
1 parent e718fb4 commit 73b83b2

8 files changed

Lines changed: 74 additions & 84 deletions

File tree

Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Time Complexity: O(n)
13+
* Space Complexity:O(1)
14+
* Optimal Time Complexity:O(n)
1515
*
1616
* @param {Array<number>} numbers - Numbers to process
1717
* @returns {Object} Object containing running total and product
1818
*/
1919
export function calculateSumAndProduct(numbers) {
2020
let sum = 0;
21-
for (const num of numbers) {
22-
sum += num;
23-
}
24-
2521
let product = 1;
2622
for (const num of numbers) {
23+
sum += num;
2724
product *= num;
2825
}
2926

@@ -32,3 +29,4 @@ export function calculateSumAndProduct(numbers) {
3229
product: product,
3330
};
3431
}
32+
console.log(calculateSumAndProduct([1, 2, 3]));
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
/**
22
* Finds common items between two arrays.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity:O(1)
5+
* Space Complexity:O(n+m)
6+
* Optimal Time Complexity:O(1)
77
*
88
* @param {Array} firstArray - First array to compare
99
* @param {Array} secondArray - Second array to compare
1010
* @returns {Array} Array containing unique common items
1111
*/
12-
export const findCommonItems = (firstArray, secondArray) => [
13-
...new Set(firstArray.filter((item) => secondArray.includes(item))),
14-
];
12+
export const findCommonItems = (firstArray, secondArray) => {
13+
const secondArr = new Set(secondArray);
14+
15+
const uniqueValues = [
16+
...new Set(firstArray.filter((item) => secondArr.has(item))),
17+
];
18+
return uniqueValues;
19+
};
20+
21+
console.log(findCommonItems([2, 5, 7], [5, 6, 7]));
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
/**
22
* Find if there is a pair of numbers that sum to a given target value.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity:O(n)
5+
* Space Complexity:O(n)
6+
* Optimal Time Complexity:O(n)
77
*
88
* @param {Array<number>} numbers - Array of numbers to search through
99
* @param {number} target - Target sum to find
1010
* @returns {boolean} True if pair exists, false otherwise
1111
*/
1212
export function hasPairWithSum(numbers, target) {
13-
for (let i = 0; i < numbers.length; i++) {
14-
for (let j = i + 1; j < numbers.length; j++) {
15-
if (numbers[i] + numbers[j] === target) {
16-
return true;
17-
}
13+
const seen = new Set();
14+
15+
for (let num of numbers) {
16+
const complement = target - num;
17+
console.log(complement);
18+
if (seen.has(complement)) {
19+
return true;
1820
}
21+
seen.add(num);
1922
}
2023
return false;
2124
}
25+
26+
console.log(hasPairWithSum([1, 2, 3], 4));
Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
11
/**
22
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity:O(n)
5+
* Space Complexity:O(n)
6+
* Optimal Time Complexity:O(n)
77
*
88
* @param {Array} inputSequence - Sequence to remove duplicates from
99
* @returns {Array} New sequence with duplicates removed
1010
*/
1111
export function removeDuplicates(inputSequence) {
12-
const uniqueItems = [];
13-
14-
for (
15-
let currentIndex = 0;
16-
currentIndex < inputSequence.length;
17-
currentIndex++
18-
) {
19-
let isDuplicate = false;
20-
for (
21-
let compareIndex = 0;
22-
compareIndex < uniqueItems.length;
23-
compareIndex++
24-
) {
25-
if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
26-
isDuplicate = true;
27-
break;
28-
}
29-
}
30-
if (!isDuplicate) {
31-
uniqueItems.push(inputSequence[currentIndex]);
32-
}
33-
}
34-
35-
return uniqueItems;
12+
return [...new Set(inputSequence)];
3613
}
14+
15+
console.log(removeDuplicates([3, 3, 5, 6, 3, 7]));

Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
1212
"sum": 10, // 2 + 3 + 5
1313
"product": 30 // 2 * 3 * 5
1414
}
15-
Time Complexity:
16-
Space Complexity:
17-
Optimal time complexity:
15+
Time Complexity:O(n)
16+
Space Complexity:O(1)
17+
Optimal time complexity:O(n)
1818
"""
1919
# Edge case: empty list
2020
if not input_numbers:
2121
return {"sum": 0, "product": 1}
2222

2323
sum = 0
24-
for current_number in input_numbers:
25-
sum += current_number
26-
2724
product = 1
2825
for current_number in input_numbers:
26+
sum += current_number
2927
product *= current_number
28+
3029

3130
return {"sum": sum, "product": product}
31+
32+
print(calculate_sum_and_product([1,2,3,4]))

Sprint-1/Python/find_common_items/find_common_items.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ def find_common_items(
99
"""
1010
Find common items between two arrays.
1111
12-
Time Complexity:
13-
Space Complexity:
14-
Optimal time complexity:
12+
Time Complexity: O(n+m)
13+
Space Complexity:O(n+m)
14+
Optimal time complexity: O(n+m)
1515
"""
16-
common_items: List[ItemType] = []
17-
for i in first_sequence:
18-
for j in second_sequence:
19-
if i == j and i not in common_items:
20-
common_items.append(i)
21-
return common_items
16+
17+
first_set = set(first_sequence)
18+
second_set = set(second_sequence)
19+
# we use the & operator shortcut, of The intersection() method
20+
# to return a set that contains the similarity between two or more sets
21+
common_items= first_set & second_set
22+
return list(common_items)
23+
24+
print(find_common_items([1,3,5,4],[1,4,8,0]))

Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
77
"""
88
Find if there is a pair of numbers that sum to a target value.
99
10-
Time Complexity:
11-
Space Complexity:
12-
Optimal time complexity:
10+
Time Complexity:O(n)
11+
Space Complexity:O(n)
12+
Optimal time complexity:O(n)
1313
"""
14-
for i in range(len(numbers)):
15-
for j in range(i + 1, len(numbers)):
16-
if numbers[i] + numbers[j] == target_sum:
17-
return True
14+
seen = set()
15+
for num in numbers:
16+
complement = target_sum - num
17+
if complement in seen:
18+
return True
19+
seen.add(num)
1820
return False
21+
print(has_pair_with_sum([1,2,3,4],3))

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
77
"""
88
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
99
10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
10+
Time complexity:O(n)
11+
Space complexity:O(n)
12+
Optimal time complexity:O(n)
1313
"""
14-
unique_items = []
1514

16-
for value in values:
17-
is_duplicate = False
18-
for existing in unique_items:
19-
if value == existing:
20-
is_duplicate = True
21-
break
22-
if not is_duplicate:
23-
unique_items.append(value)
24-
25-
return unique_items
15+
16+
# dict.fromkeys creates a dictionary with values as keys,and automatically remove duplicates
17+
#list() to keep the original order.
18+
return list(dict.fromkeys(values))
19+
print(remove_duplicates([1,2,2,3,3,4,0,0]))

0 commit comments

Comments
 (0)