Skip to content
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 @@ -12,20 +12,22 @@ 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:
Areas of inefficiency in original version:
- Two separate loops over the same list

Time Complexity: O(n)
Space Complexity: O(1)
Optimal time complexity:
"""
# Edge case: empty list
if not input_numbers:
return {"sum": 0, "product": 1}

sum = 0
for current_number in input_numbers:
sum += current_number
total_sum = 0
total_product = 1

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}
22 changes: 15 additions & 7 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ def find_common_items(
"""
Find common items between two arrays.

Time Complexity:
Space Complexity:
Optimal time complexity:
Areas of inefficiency in original version:
- Nested loops compare every element in first_sequence to every element
in second_sequence (O(n * m)).

Time Complexity: O(n + m) avrage
Space Complexity: O(m + k)
Optimal time complexity: O(n + m)
"""
second_set = set(second_sequence)
common_items: List[ItemType] = []
for i in first_sequence:
for j in second_sequence:
if i == j and i not in common_items:
common_items.append(i)
seen = set()

for item in first_sequence:
if item in second_set and item not in seen:
seen.add(item)
common_items.append(item)

return common_items
23 changes: 16 additions & 7 deletions Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
"""
Find if there is a pair of numbers that sum to a target value.

Time Complexity:
Space Complexity:
Optimal time complexity:
Areas of inefficiency in original version:
- Nested loops check all possible pairs, leading to quadratic time

Time Complexity: O(n) average
Space Complexity: O(n)
Optimal time complexity: O(n)

- set to track previously seen values.
"""
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
if numbers[i] + numbers[j] == target_sum:
return True
seen = set()

for number in numbers:
complement = target_sum - number
if complement in seen:
return True
seen.add(number)

return False
22 changes: 11 additions & 11 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
"""
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.

Time complexity:
Space complexity:
Optimal time complexity:

Areas of inefficiency in original version:
- For each value scans the growing list of unique items.

Time complexity: O(n) average
Space complexity: O(n)
Optimal time complexity: O(n)
"""
unique_items = []
seen = set()
unique_items: List[ItemType] = []

for value in values:
is_duplicate = False
for existing in unique_items:
if value == existing:
is_duplicate = True
break
if not is_duplicate:
if value not in seen:
seen.add(value)
unique_items.append(value)

return unique_items
Loading