On this page
Counter
TIP101 Unit 1 Session 1 (Click for link to problem statements)
U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What is the input?
- An integer
stop.
- An integer
- What should the function do?
- Print every number from 1 up to and including
stop, each on its own line. It does not return anything.
- Print every number from 1 up to and including
- What if
stopis less than 1?- Nothing is printed, because there are no numbers from 1 up to
stop.
- Nothing is printed, because there are no numbers from 1 up to
HAPPY CASE
Input: stop = 3
Output:
1
2
3
Explanation: The numbers 1, 2 and 3 are printed in order, including stop itself.
EDGE CASE
Input: stop = 0
Output: (nothing is printed)
Explanation: range(1, 1) is empty, so the loop body never runs.
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a function to count from 1 to stop.
1) Create a new function with a parameter for the stop value
2) For each number between 1 and stop + 1, print that value
⚠️ Common Mistakes
- Why would we go to “stop + 1” and not just “stop”?
I-mplement
def counter(stop):
for num in range(1, stop + 1):
print(num)