Skip to content

Commit 1f29d8e

Browse files
Address PR feedback
1 parent 648dc36 commit 1f29d8e

4 files changed

Lines changed: 47 additions & 33 deletions

File tree

Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@ 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(2n) = O(n) - We iterate through the list twice (once for sum, once for product)
16+
Space Complexity: O(1) - Only using constant extra space for variables
17+
Optimal time complexity: O(n) - We can achieve O(n) by combining both loops into one single pass
1818
"""
1919
# Edge case: empty list
2020
if not input_numbers:
2121
return {"sum": 0, "product": 1}
2222

23-
sum = 0
23+
sum_value = 0
24+
product_value = 1
2425
for current_number in input_numbers:
25-
sum += current_number
26+
sum_value += current_number
27+
product_value *= current_number
2628

27-
product = 1
28-
for current_number in input_numbers:
29-
product *= current_number
30-
31-
return {"sum": sum, "product": product}
29+
return {"sum": sum_value, "product": product_value}

Sprint-1/Python/find_common_items/find_common_items.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ 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) - where n is the length of the first array and m is the length of the second array.
13+
We iterate through each element in the first array and for each element, we check if it exists
14+
in the second array using the 'in' operator, which takes O(m) time. We also check if the item
15+
is already in common_items using 'not in', which takes O(k) time where k is the size of common_items.
16+
Space Complexity: O(k) - where k is the number of common items found. We store each common item in the list.
17+
Optimal time complexity: O(n + m) - Using a hash set (dictionary/set) for the second sequence allows us to achieve
18+
linear time by avoiding the nested loop. This reduces the lookup from O(m) to O(1).
1519
"""
20+
# Create a set from the second sequence for O(1) lookups
21+
second_set = set(second_sequence)
1622
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)
23+
24+
for item in first_sequence:
25+
if item in second_set and item not in common_items:
26+
common_items.append(item)
27+
2128
return common_items

Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ 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²) - where n is the length of the input array. We use two nested loops, where the outer loop
11+
iterates through each element, and the inner loop iterates through all subsequent elements.
12+
In the worst case, we need to check all pairs.
13+
Space Complexity: O(1) - We only use constant extra space for loop variables.
14+
Optimal time complexity: O(n) - Using a hash set allows us to check if the complement (target_sum - current_number)
15+
exists in O(1) time, reducing the overall complexity from O(n²) to O(n) with one pass through the array.
1316
"""
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
17+
# Optimised approach: Use a set to store seen numbers for O(1) lookups
18+
seen_numbers = set()
19+
20+
for num in numbers:
21+
complement = target_sum - num
22+
if complement in seen_numbers:
23+
return True
24+
seen_numbers.add(num)
25+
1826
return False

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ 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²) - where n is the length of the input sequence. For each element in the values sequence,
11+
we iterate through all existing items in unique_items to check if it's a duplicate.
12+
In the worst case, this requires checking all previous elements.
13+
Space Complexity: O(k) - where k is the number of unique items. We store up to k items in the unique_items list.
14+
Optimal time complexity: O(n) - Using a set to track seen items allows us to check for duplicates in O(1) time,
15+
reducing the overall complexity from O(n²) to O(n).
1316
"""
17+
# Optimised approach: Use a set to track seen items for O(1) lookups
18+
seen_items = set()
1419
unique_items = []
1520

1621
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:
22+
if value not in seen_items:
23+
seen_items.add(value)
2324
unique_items.append(value)
2425

2526
return unique_items

0 commit comments

Comments
 (0)