Skip to content

Commit f396f7a

Browse files
committed
use single loop
1 parent 810820e commit f396f7a

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,26 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
1313
"product": 30 // 2 * 3 * 5
1414
}
1515
Time Complexity:
16+
ANSWER: O(n)
17+
Because the list is looped through once.
18+
1619
Space Complexity:
20+
ANSWER: O(1)
21+
Because only two variables are used.
22+
1723
Optimal time complexity:
24+
ANSWER: O(n)
25+
Each number is processed once.
26+
1827
"""
1928
# Edge case: empty list
2029
if not input_numbers:
2130
return {"sum": 0, "product": 1}
2231

2332
sum = 0
24-
for current_number in input_numbers:
25-
sum += current_number
26-
2733
product = 1
2834
for current_number in input_numbers:
35+
sum += current_number
2936
product *= current_number
3037

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

0 commit comments

Comments
 (0)