Skip to content

Commit 2b8deef

Browse files
committed
Refactor Python array utilities to improve time complexity
1 parent 134e683 commit 2b8deef

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ 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:
15+
Areas of inefficiency in original version:
16+
- Two separate loops over the same list
17+
18+
Time Complexity: O(n)
19+
Space Complexity: O(1)
1720
Optimal time complexity:
1821
"""
1922
# Edge case: empty list
2023
if not input_numbers:
2124
return {"sum": 0, "product": 1}
2225

23-
sum = 0
24-
for current_number in input_numbers:
25-
sum += current_number
26+
total_sum = 0
27+
total_product = 1
2628

27-
product = 1
2829
for current_number in input_numbers:
29-
product *= current_number
30+
total_sum += current_number
31+
total_product *= current_number
3032

31-
return {"sum": sum, "product": product}
33+
return {"sum": total_sum, "product": total_product}

Sprint-1/Python/find_common_items/find_common_items.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ def find_common_items(
99
"""
1010
Find common items between two arrays.
1111
12-
Time Complexity:
13-
Space Complexity:
14-
Optimal time complexity:
12+
Areas of inefficiency in original version:
13+
- Nested loops compare every element in first_sequence to every element
14+
in second_sequence (O(n * m)).
15+
16+
Time Complexity: O(n + m) avrage
17+
Space Complexity: O(m + k)
18+
Optimal time complexity: O(n + m)
1519
"""
20+
second_set = set(second_sequence)
1621
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)
22+
seen = set()
23+
24+
for item in first_sequence:
25+
if item in second_set and item not in seen:
26+
seen.add(item)
27+
common_items.append(item)
28+
2129
return common_items

Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ 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+
Areas of inefficiency in original version:
11+
- Nested loops check all possible pairs, leading to quadratic time
12+
13+
Time Complexity: O(n) average
14+
Space Complexity: O(n)
15+
Optimal time complexity: O(n)
16+
17+
- set to track previously seen values.
1318
"""
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
19+
seen = set()
20+
21+
for number in numbers:
22+
complement = target_sum - number
23+
if complement in seen:
24+
return True
25+
seen.add(number)
26+
1827
return False

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
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.
9-
10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
9+
10+
Areas of inefficiency in original version:
11+
- For each value scans the growing list of unique items.
12+
13+
Time complexity: O(n) average
14+
Space complexity: O(n)
15+
Optimal time complexity: O(n)
1316
"""
14-
unique_items = []
17+
seen = set()
18+
unique_items: List[ItemType] = []
1519

1620
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:
21+
if value not in seen:
22+
seen.add(value)
2323
unique_items.append(value)
2424

2525
return unique_items

0 commit comments

Comments
 (0)