A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition
by Jay Wengrow
Chapter 10
These are the solutions to the exercises found in the section, Exercises. The solutions provided here are in Python, but you can find the solutions in JavaScript and Ruby in the code download.[10]
-
The base case is if low > high—that is, we want to stop the recursion once low has exceeded high. Otherwise, we’d end up printing numbers even greater than the high number, and onto infinity.
-
We’d have infinite recursion! factorial(10) calls factorial(8), which calls factorial(6), which calls factorial(4), which calls factorial(2), which calls factorial(0). Since our base case is if n == 1, we never end up with n ever being 1, so the recursion continues. factorial(0) calls factorial(-2), and so on.
-
Let’s say low is 1 and high is 10. When ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access