
Lab 4: Recursion, Tree Recursion, Python Lists lab04.zip
Due by 11:59pm on Wednesday, September 21.
Recursion/Tree Recursion
Q1: WWPD: 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, make sure to try drawing out the recursive call tree! This is a challenging problem ā doing so will really help with understanding how tree recursion works. We strongly encourage trying to draw things out before asking for help from your TA/AIs.
Background: In the Squared Virahanka Fibonacci sequence, each number in the sequence is the square of the sum of the previous two numbers in the sequence. The first 0th and 1st number in the sequence are 0 and 1, respectively. The recursive
virfib_sq
function takes in an argumentn
and returns thenth
number in the Square Virahanka Fibonacci sequence.
1 | >>> def virfib_sq(n): |
Q2: 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): |
Q3: 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): |
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): |
List Comprehensions
Q5: Couple
Implement the function couple
, which takes in two lists and returns a list that contains lists with i-th elements of two sequences coupled together. You can assume the lengths of two sequences are the same. Try using a list comprehension.
Hint: You may find the built in range function helpful.
1 | def couple(s, t): |
Optional Questions
Recursion
Q6: Double Eights
Write a recursive function that takes in a number n
and determines if the digits contain two adjacent 8
s. You can assume that n
is at least a two-digit number. You may have already done this problem iteratively as an Extra Practice problem in Lab 1.
Hint: Remember what tools you can use in order to isolate digits of a number. If you have trouble figuring out how to implement the recursion, try first finding an iterative solution, and think about how you might be able to turn any loops in recursion.
1 | def double_eights(n): |
List Comprehensions
Q7: Coordinates
Implement a function coords
that takes a function fn
, a sequence seq
, and a lower
and upper
bound on the output of the function. coords
then returns a list of coordinate pairs (lists) such that:
- Each (x, y) pair is represented as
[x, fn(x)]
- The x-coordinates are elements in the sequence
- The result contains only pairs whose y-coordinate is within the upper and lower bounds (inclusive)
See the doctest for examples.
Note: your answer can only be one line long. You should make use of list comprehensions!
1 | def coords(fn, seq, lower, upper): |
Q8: Riffle Shuffle
A common way of shuffling cards is known as the riffle shuffle. The shuffle produces a new configuration of cards in which the top card is followed by the middle card, then by the second card, then the card after the middle, and so forth.
Write a list comprehension that riffle shuffles a sequence of items. You can assume the sequence contains an even number of items.
Hint: There are two ways you can write this as a single list comprension: 1) You may find the expression k%2
, which evaluates to 0 on even numbers and 1 on odd numbers, to be alternatively access the beginning and middle of the deck. 2) You can utilize an if expression in your comprehension for the odd and even numbers respectively.
1 | def riffle(deck): |