Lecture example
The order of recursion
1 | def cascade(n): |
1 | def cascade1(n): |
1 | def inverse_cascade(n): |
Tree recursion
1 | def fib(n): |
1 | def count_partitions(n, m): |
More Recursion
In discussion 1, we implemented the function is_prime
, which takes in a positive integer and returns whether or not that integer is prime, iteratively.
Now, let’s implement it recursively! As a reminder, an integer is considered prime if it has exactly two unique factors: 1 and itself.
1 | # My solution, from iteration to recursion |
1 | def is_prime(n): |
Define a function make_fn_repeater
which takes in a one-argument function f
and an integer x
. It should return another function which takes in one argument, another integer. This function returns the result of applying f
to x
this number of times.
Make sure to use recursion in your solution
1 | def make_func_repeater(f, x): |
Tree Recursion
Consider a function that requires more than one recursive call. A simple example is the recursive fibonacci
function:
1 | def fib(n): |
This type of recursion is called tree recursion
, because it makes more than one recursive call in its recursive case. If we draw out the recursive calls, we see the recursive calls in the shape of an upside-down tree:
We could, in theory, use loops to write the same procedure. However, problems that are naturally solved using tree recursive procedures are generally difficult to write iteratively. It is sometimes the case that a tree recursive
problem also involves iteration: for example, you might use a while loop to add together multiple recursive calls.
As a general rule of thumb, whenever you need to try multiple possibilities at the same time, you should consider using tree recursion.
Question
I want to go up a flight of stairs that has n
steps. I can either take 1 or 2 steps each time. How many different ways can I go up this flight of stairs?
Write a function count_stair_ways
that solves this problem for me. Assume n
is positive.
Before we start, what’s the base case for this question? What is the simplest input?
When there is only one step, we have only one way to go up the stair. When there are two steps, we have two ways: take a two-step or take 2 one-steps.
What do count_stair_ways(n - 1)
and count_stair_ways(n - 2)
represent?
count_stair_ways(n - 1)
represents the number of ways to go up to the first n-1
stairs. count_stair_ways(n - 2)
represents the number of ways to go up to the first n-2
stairs.
Use those two recursive calls to write the recursive case:
1 | if n == 1: |
Consider a special version of the count_stairways
problem, where instead of taking 1 or 2 steps, we are able to take up to and including k steps at a time.
Write a function count_k
that figures out the number of paths for this scenario. Assume n
and k
are positive.
1 | def count_k(n, k): |
This seems to be like the partition code in the lecture, but when I use the way of partition to solve this problem, I find a significant bug which is that in out partition, 1 plus 2 and 2 plus 1 is considered to be the same kind, but in our stairs code, I first go up 2 stairs then 1 stair is considered to be different from first going up 1 stair and then go 2 stairs. So we need another kind of thought to solve this, which uses a loop.Video walkthrough
Here’s a part of the Pascal’s triangle:
Every number in Pascal’s triangle is defined as the sum of the item above it and the item that is directly to the upper left of it, use 0 if the entry is empty. Define the procedure pascal(row, column)
which takes a row
and a column
, and finds the value at that position in the triangle.
1 | # My solution, by observing that the number above leading diagonal is all zero. |
1 | # The pdf solution, which has a subtle difference at the condition of returning zero. |