# Lab 3: Recursion and Midterm Review

*Due at 11:59pm on Friday, 09/15/2017.*

## 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 through 3 must be completed in order to receive credit for this lab. Starter code for these questions is in lab03.py.
- Questions 4 through 10 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**: When you submit, the autograder will run tests for all questions,
including the optional questions. As long as you pass the tests for the
*required* questions, you will receive credit.

# 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:

- Base case(s), the simplest possible form of the problem you're trying to solve.
- Recursive case(s), where the function calls itself with a
*simpler argument*as part of the computation. - 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: AB+C

Implement `ab_plus_c`

, a function that takes arguments `a`

, `b`

, and
`c`

and computes `a * b + c`

. You can assume a and b are both non-negative
integers. However, you can't use the `*`

operator. Use recursion!

```
def ab_plus_c(a, b, c):
"""Computes a * b + c.
>>> ab_plus_c(2, 4, 3) # 2 * 4 + 3
11
>>> ab_plus_c(0, 3, 2) # 0 * 3 + 2
2
>>> ab_plus_c(3, 0, 2) # 3 * 0 + 2
2
"""
"*** YOUR CODE HERE ***"
if b == 0:
return c
return a + ab_plus_c(a, b - 1, c)
```

Use Ok to test your code:

`python3 ok -q ab_plus_c`

### Q2: 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`

### Q3: 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`

# Optional Questions

**Note**: The following questions are in lab03_extra.py.

## Midterm Review

### Q4: 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.

### 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`

## More Recursion Practice

### Q6: Common Misconception

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)
```

Fix the code in `lab03_extra.py`

and run:

`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)
```

### Q7: Common Misconception

Find the bugs with the following recursive functions.

```
def count_up(n):
"""Print out all numbers up to and including n in ascending order.
>>> count_up(5)
1
2
3
4
5
"""
i = 1
if i == n:
return
print(i)
i += 1
count_up(n-1)
def count_up(n):
"""Print out all numbers up to and including n in ascending order.
>>> count_up(5)
1
2
3
4
5
"""
i = 1
if i > n:
return
print(i)
i += 1
count_up(n)
```

Once you find the bugs, finish the function in `lab03_extra.py`

, and run:

`python3 ok -q count_up`

*Hint*: One possible solution uses a helper function to make recursive calls.

```
def count_up(n):
"""Print out all numbers up to and including n in ascending order.
>>> count_up(5)
1
2
3
4
5
"""
def counter(i):
"*** YOUR CODE HERE ***"
# bug: setting i = 1 in the beginning of both versions of count_up above means
# that incrementing i is useless; you will never reach your base case (unless
# n is equal to 1) because you are always resetting i
if i <= n:
print(i)
counter(i + 1) counter(1)
```

### 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 ***"
return interleaved_helper(odd_term, even_term, 1)
def interleaved_helper(term0, term1, k):
if k == n:
return term0(k)
return term0(k) + interleaved_helper(term1, term0, k+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`