Skip to content

Commit 41316b6

Browse files
author
Fatma Degirmenci
committed
Refactor calculate_sum_and_product to use a single loop for sum and product
1 parent 17975e3 commit 41316b6

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

‎Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py‎

Lines changed: 5 additions & 7 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:There are two separate loops O(n)+O(n) total --->O(n)
16+
Space Complexity:We have two variables sum and product O(1) total --->O(1)
17+
Optimal time complexity: O(n) cannot be improved because we need to process each element at least once
18+
but the code can be refactored for better readability.
1819
"""
1920
# Edge case: empty list
2021
if not input_numbers:
2122
return {"sum": 0, "product": 1}
2223

2324
sum = 0
24-
for current_number in input_numbers:
25-
sum += current_number
26-
2725
product = 1
2826
for current_number in input_numbers:
27+
sum += current_number
2928
product *= current_number
30-
3129
return {"sum": sum, "product": product}

0 commit comments

Comments
 (0)