# Lab 3: Recursion

*Due at 11:59pm on 06/30/2016.*

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

- Questions 1, 2, and 3 must be completed in order to receive credit for this lab. Starter code for these questions is in lab03.py.
- Questions 4 through 12 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

### Question 1: Common Misconception

Find the bug in the following recursive function.

```
def factorial(n):
"""Return n * (n - 1) * (n - 2) * ... * 1.
>>> factorial(5)
120
"""
if n == 0:
return 1
else:
return factorial(n-1)
```

You can unlock this question by:

`python3 ok -q factorial_ok -u`

The result of the recursive calls is not combined into the correct solution.

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

### Question 2: 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)
```

Use OK to test your code:

`python3 ok -q hailstone`

### Question 3: Symmetric List

A list is symmetric if it is the same from left to right as from right to left. For example, [1, 2, 3, 4, 3, 2, 1] is symmetric. Implement`symmetric`

, a
function that takes list `l`

and check if `l`

is a symmetric list, Use
recursion!
```
def symmetric(l):
"""Returns whether a list is symmetric.
>>> symmetric([])
True
>>> symmetric([1])
True
>>> symmetric([1, 4, 5, 1])
False
>>> symmetric([1, 4, 4, 1])
True
>>> symmetric(['l', 'o', 'l'])
True
"""
"*** YOUR CODE HERE ***"
if len(l) <= 1:
return True
elif l[0] == l[-1]:
return symmetric(l[1: -1])
else:
return False
```

Use OK to test your code:

`python3 ok -q symmetric`

# Optional Questions

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

### Question 4: 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 * 0
0
"""
if n == 0:
return 0
else:
return n * skip_mul(n - 2)
```

You can unlock this question by:

`python3 ok -q skip_mul_ok -u`

Once you unlock the question, 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 == 0`

, 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 <= 0:
return 0
elif n == 1:
return 1
else:
return n * skip_mul(n - 2)
```

### Question 5: 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)
```

You can unlock this question by:

`python3 ok -q count_up_ok -u`

Once you unlock the question, finish the function in `lab03_extra.py`

, and run:

`python3 ok -q count_up`

*Hint*: You might need a helper function to make the recursive calls with.

```
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 ***"
if i <= n:
print(i)
counter(i + 1) counter(1)
```

### Question 6: 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`

### Question 7: 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 positive
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`

### Question 8: Sublist

Write a function `has_sublist`

that takes two lists, `l`

and `sublist`

, and returns whether the elements of `sublist`

appear in order anywhere within `l`

.

```
def has_sublist(l, sublist):
"""Returns whether the elements of sublist appear in order anywhere within list l.
>>> has_sublist([], [])
True
>>> has_sublist([3, 3, 2, 1], [])
True
>>> has_sublist([], [3, 3, 2, 1])
False
>>> has_sublist([3, 3, 2, 1], [3, 2, 1])
True
>>> has_sublist([3, 2, 1], [3, 2, 1])
True
"""
sublist_length = len(sublist)
l_length = len(l)
"*** YOUR CODE HERE ***"
if sublist_length > l_length:
return False
elif l[0: sublist_length] == sublist:
return True
else:
return has_sublist(l[1:], sublist)
```

Use OK to test your code:

`python3 ok -q has_sublist`

### Question 9: Sort a list

Sorting is a very important topic in computer science. Let's try sorting a list in this question.

First, let's write a function `remove_first`

that takes in a list `lst`

, and
removes the first appearance of the number `elem`

. Use recursion!

```
def remove_first(lst, elem):
""" This function removes the first appearance of elem in list lst.
>>> remove_first([3, 4] , 3)
[4]
>>> remove_first([3, 4, 3] , 3)
[4, 3]
>>> remove_first([2, 4] , 3)
[2, 4]
>>> remove_first([] , 0)
[]
"""
"*** YOUR CODE HERE ***"
if lst == []:
return lst
elif lst[0] == elem:
return lst[1:]
else:
return lst[:1] + remove_first(lst[1:], elem)
```

Use OK to test your code:

`python3 ok -q remove_first`

Next, write a function `sort`

that takes in a list `lst`

and return the sorted
version of that list. Use recursion!

Hint: Use the`remove_first`

function you just defined, and the built-in`min`

function.

```
def sort(lst):
"""This function returns a sorted version of the list lst.
>>> sort([6, 2, 5])
[2, 5, 6]
>>> sort([2, 3])
[2, 3]
>>> sort([3])
[3]
>>> sort([])
[]
"""
"*** YOUR CODE HERE ***"
if len(lst) <= 1:
return lst
else:
small = min(lst)
new_lst = remove_first(lst,small)
return [small] + sort(new_lst)
```

Use OK to test your code:

`python3 ok -q sort`

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

Use OK to test your code:

`python3 ok -q interleaved_sum`

### Question 11: 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`

### Question 12: 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 the project 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 < sqrt(n): #could replace with i == 1
return True
if n % i == 0:
return False
return helper(i - 1)
return helper(n - 1)
```

Use OK to test your code:

`python3 ok -q is_prime`