Lab 3: Recursion and Midterm Review

Due at 11:59pm on Friday, 02/09/2018.

Starter Files

Download lab03.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the Ok autograder.

Submission

By the end of this lab, you should have submitted the lab with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be graded. Check that you have successfully submitted your code on okpy.org.

  • Questions 1 and 2 must be completed in order to receive credit for this lab. Starter code for these questions is in lab03.py.
  • Questions 3 through 10 are for midterm review and are optional. It is recommended that you complete these problems on your own time. Starter code for these questions is in the lab03_extra.py file.

Note: In order to help you prepare for the midterm, we've provided the solutions to the problems in this lab assignment. You may consult them if you get stuck or to check your answers, but please try to solve them on your own first. You must still submit the required questions by Friday night if you want to receive credit for this lab assignment.

Topics

Consult this section if you need a refresher on the material for this lab. It's okay to skip directly to the questions and refer back here should you get stuck.

Recursion

A recursive function is a function that calls itself in its body, either directly or indirectly. Recursive functions have three important components:

  1. Base case(s), the simplest possible form of the problem you're trying to solve.
  2. Recursive case(s), where the function calls itself with a simpler argument as part of the computation.
  3. Using the recursive calls to solve the full problem.

Let's look at the canonical example, factorial:

def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)

We know by its definition that 0! is 1. So we choose n == 0 as our base case. The recursive step also follows from the definition of factorial, i.e., n! = n * (n-1)!.

The next few questions in lab will have you writing recursive functions. Here are some general tips:

  • Consider how you can solve the current problem using the solution to a simpler version of the problem. Remember to trust the recursion: assume that your solution to the simpler problem works correctly without worrying about how.
  • Think about what the answer would be in the simplest possible case(s). These will be your base cases - the stopping points for your recursive calls. Make sure to consider the possibility that you're missing base cases (this is a common way recursive solutions fail).
  • It may help to write the iterative version first.

Required Questions

Q1: GCD

The greatest common divisor of two positive integers a and b is the largest integer which evenly divides both numbers (with no remainder). Euclid, a Greek mathematician in 300 B.C., realized that the greatest common divisor of a and b is one of the following:

  • the smaller value if it evenly divides the larger value, or
  • the greatest common divisor of the smaller value and the remainder of the larger value divided by the smaller value

In other words, if a is greater than b and a is not divisible by b, then

gcd(a, b) = gcd(b, a % b)

Write the gcd function recursively using Euclid's algorithm.

def gcd(a, b):
    """Returns the greatest common divisor of a and b.
    Should be implemented using recursion.

    >>> gcd(34, 19)
    1
    >>> gcd(39, 91)
    13
    >>> gcd(20, 30)
    10
    >>> gcd(40, 40)
    40
    """
"*** YOUR CODE HERE ***"
a, b = max(a, b), min(a, b) if a % b == 0: return b else: return gcd(b, a % b) # Iterative solution, if you're curious def gcd_iter(a, b): """Returns the greatest common divisor of a and b, using iteration. >>> gcd_iter(34, 19) 1 >>> gcd_iter(39, 91) 13 >>> gcd_iter(20, 30) 10 >>> gcd_iter(40, 40) 40 """ if a < b: return gcd_iter(b, a) while a > b and not a % b == 0: a, b = b, a % b return b

Use Ok to test your code:

python3 ok -q gcd

Q2: Hailstone

For the hailstone function from homework 1, you pick a positive integer n as the start. If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. Repeat this process until n is 1. Write a recursive version of hailstone that prints out the values of the sequence and returns the number of steps.

def hailstone(n):
    """Print out the hailstone sequence starting at n, and return the
    number of elements in the sequence.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
"*** YOUR CODE HERE ***"
print(n) if n == 1: return 1 elif n % 2 == 0: return 1 + hailstone(n // 2) else: return 1 + hailstone(3 * n + 1) # Alternate solution with helper function def hailstone2(n): def hail_helper(n, count): print(n) if n == 1: return count else: if n % 2 == 0: return hail_helper(n // 2, count + 1) else: return hail_helper(3 * n + 1, count + 1) return hail_helper(n, 1)

Use Ok to test your code:

python3 ok -q hailstone

Midterm Review

Note: The following questions are in lab03_extra.py.

Functions

Q3: WWPD: Call Expressions

Use Ok to test your knowledge with the following "What Would Python Display?" questions:

python3 ok -q call_expressions -u

For all WWPD questions, type Function if you believe the answer is <function...>, > Error if it errors, and Nothing if nothing is displayed.

>>> from operator import add
>>> def double(x):
...     return x + x
...
>>> def square(y):
...     return y * y
...
>>> def f(z):
...     add(square(double(z)), 1)
...
>>> f(4)
______
# f(4) returns None, which is a special value that the interpreter hides unless explicitly printed
>>> def foo(x, y):
...     print("x or y")
...     return x or y
...
>>> a = foo
______
# We aren't calling foo here; we are just binding the variable a in the global frame to the function object foo
>>> b = foo()
______
TypeError: foo() missing 2 required positional arguments: 'x' and 'y'
>>> c = a(print("x"), print("y"))
______
x y x or y
>>> print(c)
______
None
>>> def welcome():
...     print('welcome to')
...     return 'hello'
...
>>> def cs61a():
...     print('cs61a')
...     return 'world'
...
>>> print(welcome(), cs61a())
______
welcome to cs61a hello world

Higher Order Functions

Q4: I Heard You Liked Functions...

Define a function cycle that takes in three functions f1, f2, f3, as arguments. cycle will return another function that should take in an integer argument n and return another function. That final function should take in an argument x and cycle through applying f1, f2, and f3 to x, depending on what n was. Here's what the final function should do to x for a few values of n:

  • n = 0, return x
  • n = 1, apply f1 to x, or return f1(x)
  • n = 2, apply f1 to x and then f2 to the result of that, or return f2(f1(x))
  • n = 3, apply f1 to x, f2 to the result of applying f1, and then f3 to the result of applying f2, or f3(f2(f1(x)))
  • n = 4, start the cycle again applying f1, then f2, then f3, then f1 again, or f1(f3(f2(f1(x))))
  • And so forth.

Hint: most of the work goes inside the most nested function.

def cycle(f1, f2, f3):
    """Returns a function that is itself a higher-order function.

    >>> def add1(x):
    ...     return x + 1
    >>> def times2(x):
    ...     return x * 2
    >>> def add3(x):
    ...     return x + 3
    >>> my_cycle = cycle(add1, times2, add3)
    >>> identity = my_cycle(0)
    >>> identity(5)
    5
    >>> add_one_then_double = my_cycle(2)
    >>> add_one_then_double(1)
    4
    >>> do_all_functions = my_cycle(3)
    >>> do_all_functions(2)
    9
    >>> do_more_than_a_cycle = my_cycle(4)
    >>> do_more_than_a_cycle(2)
    10
    >>> do_two_cycles = my_cycle(6)
    >>> do_two_cycles(1)
    19
    """
"*** YOUR CODE HERE ***"
def ret_fn(n): def ret(x): i = 0 while i < n: if i % 3 == 0: x = f1(x) elif i % 3 == 1: x = f2(x) else: x = f3(x) i += 1 return x return ret return ret_fn

Use Ok to test your code:

python3 ok -q cycle

Lambda expressions

Q5: Palindrome

A number is considered a palindrome if it reads the same forwards and backwards. Fill in the blanks '_' to help determine if a number is a palindrome. In the spirit of exam style questions, please do not edit any parts of the function other than the blanks.

def is_palindrome(n):
    """
    Fill in the blanks '_____' to check if a number
    is a palindrome.

    >>> is_palindrome(12321)
    True
    >>> is_palindrome(42)
    False
    >>> is_palindrome(2015)
    False
    >>> is_palindrome(55)
    True
    """
    x, y = n, 0
f = lambda: _____
f = lambda: y * 10 + x % 10
while x > 0:
x, y = _____, f()
x, y = x // 10, f()
return y == n

Use Ok to test your code:

python3 ok -q is_palindrome

Environment diagrams

Q6: Doge

Draw the environment diagram for the following code.

wow = 6

def much(wow):
    if much == wow:
        such = lambda wow: 5
        def wow():
            return such
        return wow
    such = lambda wow: 4
    return wow()

wow = much(much(much))(wow)

Verify your solution with Python Tutor.

More Recursion Practice

Q7: Find the Bug

Find the bug with this recursive function.

def skip_mul(n):
    """Return the product of n * (n - 2) * (n - 4) * ...

    >>> skip_mul(5) # 5 * 3 * 1
    15
    >>> skip_mul(8) # 8 * 6 * 4 * 2
    384
    """
    if n == 2:
        return 2
    else:
        return n * skip_mul(n - 2)

When you find it, run this to check your answer:

python3 ok -q skip_mul_ok -u

Then, fix the code in lab03_extra.py and run this to check your code:

python3 ok -q skip_mul

Consider what happens when we choose an odd number for n. skip_mul(3) will return 3 * skip_mul(1). skip_mul(1) will return 1 * skip_mul(-1). You may see the problem now. Since we are decreasing n by two at a time, we've completed missed our base case of n == 2, and we will end up recursing indefinitely. We need to add another base case to make sure this doesn't happen.

def skip_mul(n):
    if n == 1:
        return 1
    elif n == 2:
        return 2
    else:
        return n * skip_mul(n - 2)

Q8: Is Prime

Write a function is_prime that takes a single argument n and returns True if n is a prime number and False otherwise. Assume n > 1. We implemented this in Discussion 1 iteratively, now time to do it recursively!

Hint: You will need a helper function! Remember helper functions are useful if you need to keep track of more variables than the given parameters, or if you need to change the value of the input.

def is_prime(n):
    """Returns True if n is a prime number and False otherwise.

    >>> is_prime(2)
    True
    >>> is_prime(16)
    False
    >>> is_prime(521)
    True
    """
"*** YOUR CODE HERE ***"
def helper(i): if i > (n ** 0.5): # Could replace with i == n return True elif n % i == 0: return False return helper(i + 1) return helper(2)

Use Ok to test your code:

python3 ok -q is_prime

Q9: Interleaved Sum

Recall that the summation function computes the sum of a sequence of terms from 1 to n:

def summation(n, term):
    """Return the sum of the first n terms of a sequence.

    >>> summation(5, lambda x: pow(x, 3))
    225
    """
    total, k = 0, 1
    while k <= n:
        total, k = total + term(k), k + 1
    return total

Write a function interleaved_sum that similarly computes the sum of a sequence of terms from 1 to n, but uses different functions to compute the terms for odd and even numbers. Do so without using any loops or testing in any way if a number is odd or even. (You may test if a number is equal to 0, 1, or n.)

Hint: Use recursion and a helper function!

def interleaved_sum(n, odd_term, even_term):
    """Compute the sum odd_term(1) + even_term(2) + odd_term(3) + ..., up
    to n.

    >>> # 1 + 2^2 + 3 + 4^2 + 5
    ... interleaved_sum(5, lambda x: x, lambda x: x*x)
    29
    """
"*** YOUR CODE HERE ***"
def interleaved_helper(term0, term1, k): if k == n: return term0(k) return term0(k) + interleaved_helper(term1, term0, k+1) return interleaved_helper(odd_term, even_term, 1) # Alternate solution def interleaved_sum2(n, odd_term, even_term): """Compute the sum odd_term(1) + even_term(2) + odd_term(3) + ..., up to n. >>> # 1 + 2^2 + 3 + 4^2 + 5 ... interleaved_sum2(5, lambda x: x, lambda x: x*x) 29 """ total, term0, term1 = interleaved_helper2(n, odd_term, even_term) return total def interleaved_helper2(n, odd_term, even_term): if n == 1: return odd_term(1), even_term, odd_term else: total, term0, term1 = interleaved_helper2(n-1, odd_term, even_term) return total + term0(n), term1, term0 # Alternate solution using odd_term and even_term separately def interleaved_sum3(n, odd_term, even_term): def interleaved_helper3(i, fn): if i > n: return 0 return fn(i) + interleaved_helper3(i + 2, fn) return interleaved_helper3(0, even_term) + interleaved_helper3(1, odd_term)

Use Ok to test your code:

python3 ok -q interleaved_sum

Q10: Ten-pairs

Write a function that takes a positive integer n and returns the number of ten-pairs it contains. A ten-pair is a pairs of digits within n that sum to 10. Do not use any assignment statements.

The number 7,823,952 has 3 ten-pairs. The first and fourth digits sum to 7+3=10, the second and third digits sum to 8+2=10, and the second and last digit sum to 8+2=10:

Hint: Use a helper function to calculate how many times a digit appears in n.

def ten_pairs(n):
    """Return the number of ten-pairs within positive integer n.

    >>> ten_pairs(7823952)
    3
    >>> ten_pairs(55055)
    6
    >>> ten_pairs(9641469)
    6
    """
"*** YOUR CODE HERE ***"
if n < 10: return 0 else: return ten_pairs(n//10) + count_digit(n//10, 10 - n%10) def count_digit(n, digit): """Return how many times digit appears in n. >>> count_digit(55055, 5) 4 """ if n == 0: return 0 else: if n%10 == digit: return count_digit(n//10, digit) + 1 else: return count_digit(n//10, digit)

Use Ok to test your code:

python3 ok -q ten_pairs