These are the solutions to the exercises found in the section, Exercises . The solutions provided here are in Ruby, but you can find the solutions in JavaScript and Python in the code download. [11]
-
Let’s call our function character_count . The first step is to pretend that the character_count function has already been implemented.
Next, we need to identify the subproblem. If our problem is the array, ["ab", "c", "def", "ghij"] , then our subproblem can be the same array missing one string. Let’s specifically say that our subproblem is the array minus the first string, which would be ["c", "def", "ghij"] .
Now, let’s see what happens when we apply the “already implemented” function on the subproblem. If we were to call character_count(["c", "def", "ghij"]) , we’d get a return value of 8 , since there are eight characters in total.
So, to solve our original problem, all we have to do is add the length of the first string ( "ab" ) to the result of calling the character_count function on the subproblem.
Here’s one possible implementation:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>character_count</strong> (array)</td></tr><tr><td> <span> </span></td><td> <em># Alternative base case:</em> </td></tr><tr><td> <span> </span></td><td> <em># return array[0].length if array.length == 1</em> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <em># Base case: when the array is empty:</em> </td></tr><tr><td> <span> </span></td><td> <strong>return</strong> 0 <strong>if</strong> array. <strong>length</strong> == 0</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>return</strong> array[0]. <strong>length</strong> + character_count(array[1, array. <strong>length</strong> - 1])</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>
Note that we made our base case to be when the array is empty, in which case there are zero string characters. We mention an alternative and equally viable base case in the comments, which is when the array contains one string. In that case, we return the length of that single string.
-
First, let’s pretend the select_even function already works. Next, let’s identify the subproblem. If we try to select all the even numbers in the example array, [1, 2, 3, 4, 5] , we could say that the subproblem is all the numbers in the array beside the first one. So, let’s imagine select_even([2, 3, 4, 5]) already works and returns [2, 4] .
Since the first number in the array is 1 , we actually don’t want to do anything other than return the [2, 4] . However, if the first number in the array was a 0, we’d want to return the [2, 4] with the 0 added to it.
The base case can be when the array is empty.
Here’s one possible implementation:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>select_even</strong> (array)</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> [] <strong>if</strong> array. <strong>empty?</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>if</strong> array[0]. <strong>even?</strong> </td></tr><tr><td> <span> </span></td><td> <strong>return</strong> [array[0]] + select_even(array[1, array. <strong>length</strong> - 1])</td></tr><tr><td> <span> </span></td><td> <strong>else</strong> </td></tr><tr><td> <span> </span></td><td> <strong>return</strong> select_even(array[1, array. <strong>length</strong> - 1])</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>
-
The definition of a triangular number was given as n plus the previous number from the pattern. If the name of our function is triangle , we can express this simply as n + triangle(n - 1) . The base case is when n is 1.
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>triangle</strong> (n)</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> 1 <strong>if</strong> n == 1</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> n + triangle(n - 1)</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>
-
Let’s pretend our function, index_of_x , has already been implemented. Next, let’s say the subproblem is our string minus its first character. For example, if our input string is "hex" , the subproblem is "ex" .
Now, index_of_x("ex") would return 1 . To calculate the index of the "x" for the original string, we would add 1 to this since the additional "h" at the front of the string moves the "x" down one index.
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>index_of_x</strong> (string)</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> 0 <strong>if</strong> string[0] == <em>'x'</em> </td></tr><tr><td> <span> </span></td><td> <strong>return</strong> index_of_x(string[1, string. <strong>length</strong> - 1]) + 1</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>
-
This exercise is similar to the “Staircase Problem.” Let’s break this down:
From the starting position, we have only two choices of movement. We can either move one space to the right or one space downward.
What this means is that the total number of unique shortest paths will be the umber of paths from space to the right of S + the number of paths from space below S.
The number of paths from the space to the right of S is the same as calculating the paths in a grid of six columns and three rows, as you can see here:
The number of paths from the space below the S is the equivalent of the paths in a grid of seven columns and two rows:
Recursion allows us to express this beautifully:
<table><tbody><tr><td> <span> </span></td><td> <strong>return</strong> unique_paths(rows - 1, columns) + unique_paths(rows, columns - 1)</td></tr></tbody></table>
All we need to do now is add the base case. Possible base cases include when we have just one row or one column, since in such cases, there’s only one path available to us.
Here’s the complete function:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>unique_paths</strong> (rows, columns)</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> 1 <strong>if</strong> rows == 1 || columns == 1</td></tr><tr><td> <span> </span></td><td> <strong>return</strong> unique_paths(rows - 1, columns) + unique_paths(rows, columns - 1)</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>