August 2020
Intermediate to advanced
508 pages
11h 53m
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) |
| | return if low > high |
| | puts low |
| | print_every_other(low + 2, high) |
| | end |
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). Predict what will happen when we run factorial(10) using this function:
| | def factorial(n) |
| | return 1 if n == 1 |
| | return ... |
Read now
Unlock full access