CS61A Lab 4
Lab 4: Recursion, Tree Recursion lab04.zip
What Would Python Do?
Q1: Squared Virahanka Fibonacci
Use Ok to test your knowledge with the following “What Would Python Display?” questions:
1 python3 ok -q squared-virfib-wwpd -u✂️
Hint: If you are stuck, try drawing out the recursive call tree. See 02/11’s Lecture (Tree Recursion) for more information.
1 | def virfib_sq(n): |
Parsons Problems
To work on these problems, open the Parsons editor:
1 | python3 parsons |
Q2: Line Stepper
Complete the function line_stepper
, which returns the number of ways there are to go from start
to 0 on the number line by taking exactly k
steps along the number line. Note that at each step, you must travel either left or right; you may not stay in place!
For example, here is a visualization of all possible paths if we start at 3
on the number line with 5
steps. At every step, we move either one step to the left of right, and we ultimately end each path at 0.
1 | def line_stepper(start, k): |
Code Writing Questions
Q3: Summation
Write a recursive implementation of summation
, which takes a positive integer n
and a function term
. It applies term
to every number from 1
to n
including n
and returns the sum.
Important: Use recursion; the tests will fail if you use any loops (for, while).
1 | def summation(n, term): |
Use Ok to test your code:
1 | python3 ok -q summation✂️ |
Q4: Insect Combinatorics
Consider an insect in an M by N grid. The insect starts at the bottom left corner, (1, 1), and wants to end up at the top right corner, (M, N). The insect is only capable of moving right or up. Write a function paths
that takes a grid length and width and returns the number of different paths the insect can take from the start to the goal. (There is a closed-form solution to this problem, but try to answer it procedurally using recursion.)
For example, the 2 by 2 grid has a total of two ways for the insect to move from the start to the goal. For the 3 by 3 grid, the insect has 6 diferent paths (only 3 are shown above).
Hint: What happens if we hit the top or rightmost edge?
1 | def paths(m, n): |
Use Ok to test your code:
1 | python3 ok -q paths✂️ |
Q5: Pascal’s Triangle
Pascal’s triangle gives the coefficients of a binomial expansion; if you expand the expression (a + b) ** n
, all coefficients will be found on the n
th row of the triangle, and the coefficient of the i
th term will be at the i
th column.
Here’s a part of the Pascal’s trangle:
1 | 1 |
Every number in Pascal’s triangle is defined as the sum of the item above it and the item above and to the left of it. Rows and columns are zero-indexed; that is, the first row is row 0 instead of 1 and the first column is column 0 instead of column 1. For example, the item at row 2, column 1 in Pascal’s triangle is 2.
Now, define the procedure pascal(row, column)
which takes a row and a column, and finds the value of the item at that position in Pascal’s triangle. Note that Pascal’s triangle is only defined at certain areas; use 0
if the item does not exist. For the purposes of this question, you may also assume that row >= 0
and column >= 0
.
1 | def pascal(row, column): |
Use Ok to test your code:
1 | python3 ok -q pascal✂️ |