December 2023
Intermediate to advanced
504 pages
11h 43m
English
The following exercises provide you with the opportunity to practice with recursion. The solutions to these exercises are found in the section Chapter 10.
The following function prints every other number from a low number to a high number. For example, if low is 0 and high is 10, it would print:
| | 0 |
| | 2 |
| | 4 |
| | 6 |
| | 8 |
| | 10 |
Identify the base case in the function:
| | def print_every_other(low, high): |
| | if low > high: |
| | return |
| | |
| | print(low) |
| | print_every_other(low + 2, high) |
My kid was playing with my computer and changed my factorial function so that it computes factorial based on (n - 2) instead of (n - 1). He also changed number <= 1 to number == 1. Predict what will happen when we run factorial(10) using this function:
| | def ... |
Read now
Unlock full access