
Homework 3: Recursion, Tree Recursion hw03.zip
Q1: Num eights
Write a recursive function num_eights
that takes a positive integer pos
and returns the number of times the digit 8 appears in pos
.
Important: Use recursion; the tests will fail if you use any assignment statements or loops. (You can however use function definitions if you so wish.)
1 | def num_eights(pos): |
Q2: Ping-pong
The ping-pong sequence counts up starting from 1 and is always either counting up or counting down. At element k
, the direction switches if k
is a multiple of 8 or contains the digit 8. The first 30 elements of the ping-pong sequence are listed below, with direction swaps marked using brackets at the 8th, 16th, 18th, 24th, and 28th elements:
Index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | [8] | 9 | 10 | 11 | 12 | 13 | 14 | 15 | [16] | 17 | [18] | 19 | 20 | 21 | 22 | 23 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PingPong Value | 1 | 2 | 3 | 4 | 5 | 6 | 7 | [8] | 7 | 6 | 5 | 4 | 3 | 2 | 1 | [0] | 1 | [2] | 1 | 0 | -1 | -2 | -3 |
Index (cont.) | [24] | 25 | 26 | 27 | [28] | 29 | 30 |
---|---|---|---|---|---|---|---|
PingPong Value | [-4] | -3 | -2 | -1 | [0] | -1 | -2 |
Implement a function pingpong
that returns the nth element of the ping-pong sequence without using any assignment statements. (You are allowed to use function definitions.)
You may use the function num_eights
, which you defined in the previous question.
Important: Use recursion; the tests will fail if you use any assignment statements. (You can however use function definitions if you so wish.)
Hint: If you’re stuck, first try implementing
pingpong
using assignment statements and awhile
statement. Then, to convert this into a recursive solution, write a helper function that has a parameter for each variable that changes values in the body of the while loop.Hint: There are a few pieces of information that we need to keep track of. One of these details is the direction that we’re going (either increasing or decreasing). Building off of the hint above, think about how we can keep track of the direction throughout the calls to the helper function.
1 | def pingpong(n): |
Q3: Count coins
Given a positive integer change
, a set of coins makes change for change
if the sum of the values of the coins is change
. Here we will use standard US Coin values: 1, 5, 10, 25. For example, the following sets make change for 15
:
- 15 1-cent coins
- 10 1-cent, 1 5-cent coins
- 5 1-cent, 2 5-cent coins
- 5 1-cent, 1 10-cent coins
- 3 5-cent coins
- 1 5-cent, 1 10-cent coin
Thus, there are 6 ways to make change for 15
. Write a recursive function count_coins
that takes a positive integer change
and returns the number of ways to make change for change
using coins.
You can use either of the functions given to you:
next_larger_coin
will return the next larger coin denomination from the input, i.e.next_larger_coin(5)
is10
.next_smaller_coin
will return the next smaller coin denomination from the input, i.e.next_smaller_coin(5)
is1
.- Either function will return
None
if the next coin value does not exist
There are two main ways in which you can approach this problem. One way uses next_larger_coin
, and another uses next_smaller_coin
.
Important: Use recursion; the tests will fail if you use loops.
Hint: Refer the implementation of
count_partitions
for an example of how to count the ways to sum up to a final value with smaller parts. If you need to keep track of more than one value across recursive calls, consider writing a helper function.
1 | def next_larger_coin(coin): |