-
Notifications
You must be signed in to change notification settings - Fork 243
Total Sum in Range
Sar Champagne Bielert edited this page Apr 8, 2024
·
6 revisions
Unit 1 Session A (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Will
start
andstop
always be positive integers?- Yes.
- Will
start
ever be larger thanstop
?- No.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a function that takes parameters start
to stop
and returns the sum of the numbers from start
to stop
.
1) Create a variable to store the total sum
2) Loop through the numbers "start" to "stop", and add each to the sum variable
3) Return the sum variable
- Make sure your loop includes the first number and the final number!
def sum_range(start, stop):
total = 0
for num in range (start, stop + 1):
total += num
return total