Skip to content

Commit 9db3262

Browse files
committed
These are the implementations of the this part of assignment.
1 parent 05e5a49 commit 9db3262

4 files changed

Lines changed: 33 additions & 18 deletions

File tree

Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
1616
Space Complexity:
1717
Optimal time complexity:
1818
"""
19+
20+
"""
21+
In this case, there is no changes in the functionality that can change the time complexity as the complexity is already O(n) and only extra space is used.
22+
23+
In this case, I can just make the code easier to read and cleaner.
24+
"""
1925
# Edge case: empty list
2026
if not input_numbers:
2127
return {"sum": 0, "product": 1}
2228

2329
sum = 0
24-
for current_number in input_numbers:
25-
sum += current_number
26-
2730
product = 1
2831
for current_number in input_numbers:
32+
sum += current_number
2933
product *= current_number
3034

3135
return {"sum": sum, "product": product}

Sprint-1/Python/find_common_items/find_common_items.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ def find_common_items(
1313
Space Complexity:
1414
Optimal time complexity:
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)
16+
17+
""" In this case, the time complexity is O(n * m), it will only become O(n^2) when the two arrays have equal lengths. This case easily be avoided by using a set to store the second sequence. When checking for a match in the set using item in second-set, this has a time complexity of O(1).
18+
19+
"""
20+
21+
second_set = set(second_sequence)
22+
common_items = []
23+
24+
for item in first_sequence:
25+
26+
if item in second_set:
27+
common_items.append(item)
2128
return common_items

Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
1111
Space Complexity:
1212
Optimal time complexity:
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+
for i in range(len(numbers) - 1):
15+
16+
if numbers[i] + numbers[i + 1] == target_sum:
17+
return True
18+
1819
return False

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
1111
Space complexity:
1212
Optimal time complexity:
1313
"""
14+
15+
"""
16+
The time complexity in the new implementation is O(n) because we only loop and iterated through the array once. The use of set to add and check value for occurrence is O(1) which is the best Optimal time complexity
17+
"""
18+
1419
unique_items = []
20+
new_set = set()
1521

1622
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+
24+
if value not in new_set:
2325
unique_items.append(value)
26+
new_set.add(value)
2427

2528
return unique_items

0 commit comments

Comments
 (0)