The following exercises provide you with the opportunity to practice with recursion. The solutions to these exercises are found in the section, ​ Chapter 10 ​ .

  1. 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:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>0</td></tr><tr><td>​ <span>&nbsp;</span></td><td>2</td></tr><tr><td>​ <span>&nbsp;</span></td><td>4</td></tr><tr><td>​ <span>&nbsp;</span></td><td>6</td></tr><tr><td>​ <span>&nbsp;</span></td><td>8</td></tr><tr><td>​ <span>&nbsp;</span></td><td>10</td></tr></tbody></table>

    Identify the base case in the function:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>def</strong> ​ ​ <strong>print_every_other</strong> ​(low, high)</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ ​ <strong>if</strong> ​ low &gt; high</td></tr><tr><td>​ <span>&nbsp;</span></td><td>puts low</td></tr><tr><td>​ <span>&nbsp;</span></td><td>print_every_other(low + 2, high)</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>end</strong> ​</td></tr></tbody></table>

  2. 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:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>def</strong> ​ ​ <strong>factorial</strong> ​(n)</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ 1 ​ <strong>if</strong> ​ n == 1</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ n * factorial(n - 2)</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>end</strong> ​</td></tr></tbody></table>

  3. Following is a function in which we pass in two numbers called low and high . The function returns the sum of all the numbers from low to high . For example, if low is 1, and high is 10, the function will return the sum of all numbers from 1 to 10, which is 55. However, our code is missing the base case, and will run indefinitely! Fix the code by adding the correct base case:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>def</strong> ​ ​ <strong>sum</strong> ​(low, high)</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ high + sum(low, high - 1)</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>end</strong> ​</td></tr></tbody></table>

  4. Here is an array containing both numbers as well as other arrays, which in turn contain numbers and arrays:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>array = [ 1,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>2,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>3,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>[4, 5, 6],</td></tr><tr><td>​ <span>&nbsp;</span></td><td>7,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>[8,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>[9, 10, 11,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>[12, 13, 14]</td></tr><tr><td>​ <span>&nbsp;</span></td><td>]</td></tr><tr><td>​ <span>&nbsp;</span></td><td>],</td></tr><tr><td>​ <span>&nbsp;</span></td><td>[15, 16, 17, 18, 19,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>[20, 21, 22,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>[23, 24, 25,</td></tr><tr><td>​ <span>&nbsp;</span></td><td>[26, 27, 29]</td></tr><tr><td>​ <span>&nbsp;</span></td><td>], 30, 31</td></tr><tr><td>​ <span>&nbsp;</span></td><td>], 32</td></tr><tr><td>​ <span>&nbsp;</span></td><td>], 33</td></tr><tr><td>​ <span>&nbsp;</span></td><td>]</td></tr></tbody></table>

    Write a recursive function that prints all the numbers (and just numbers).

Copyright © 2020, The Pragmatic Bookshelf.