# Lab 3: Recursion and Python Lists lab03.zip

Due at 11:59pm on Friday, 06/29/2018.

Lab Check-in 2 questions here.

## 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-3 must be completed in order to receive credit for this lab. Starter code for these questions is in lab03.py.
• Questions 4-7 are optional, but highly recommended if you have the time. Starter code for these questions is in the lab03_extra.py file.

# Topics

Consult this section if you are unfamiliar with the material for this lab. It's okay to skip directly to the questions and refer back here if 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`.

Factorial, denoted with the `!` operator, is defined as:

``n! = n * (n-1) ... 1``

The recursive implementation for factorial is as follows:

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

We know from 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:

• Paradoxically, to write a recursive function, you must assume that the function is fully functional before you finish writing it; this is called the recursive leap of faith.
• Consider how you can solve the current problem using the solution to a simpler version of the problem. The amount of work done in a recursive function can be deceptively little: remember to take the leap of faith and trust the recursion to solve the slightly smaller problem 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.

## Lists

Lists are Python data structures that can store multiple values. Each value can be any type and can even be another list! A list is written as a commma separated list of expressions within square brackets:

``````>>> list_of_nums = [1, 2, 3, 4]
>>> list_of_bools = [True, True, False, False]
>>> nested_lists = [1, [2, 3], [4, [5]]]``````

Each element in a list is assigned an index. Lists are zero-indexed, meaning their indices start at `0` and increase in sequential order. To retrieve an element from a list, use list indexing:

``````>>> lst = [6, 5, 4, 3, 2, 1]
>>> lst[0]
6
>>> lst[3]
3``````

Often times we need to know how long a list is when we're working with it. To find the length of a list, call the function `len` on it:

``````>>> len([])
0
>>> len([2, 4, 6, 8, 10])
5``````

Tip: Recall that empty lists, `[]`, are false-y values. Therefore, you can use an if statement like the following if you only want to do operations on non-empty lists:

``````if lst:
# Do stuff with the elements of list``````

This is equivalent to:

``````if len(lst) > 0:
# Do stuff``````

You can also create a copy of some portion of the list using list slicing. To slice a list, use this syntax: `lst[<start index>:<end index>]`. This expression evaluates to a new list containing the elements of `lst` starting at and including the element at `<start index>` up to but not including the element at `end index`.

``````>>> lst = [True, False, True, True, False]
>>> lst[1:4]
[False, True, True]
>>> lst[:3]  # Start index defaults to 0
[True, False, True]
>>> lst[3:]  # End index defaults to len(lst) + 1
[True, False]
>>> lst[:]  # Creates a copy of the whole list
[True, False, True, True, False]``````

### List Comprehensions

List comprehensions are a compact and powerful way of creating new lists out of sequences. The general syntax for a list comprehension is the following:

``[<expression> for <element> in <sequence> if <conditional>]``

The syntax is designed to read like English: "Compute the expression for each element in the sequence if the conditional is true for that element."

Let's see it in action:

``````>>> [i**2 for i in [1, 2, 3, 4] if i % 2 == 0]
[4, 16]``````

Here, for each element `i` in `[1, 2, 3, 4]` that satisfies `i % 2 == 0`, we evaluate the expression `i**2` and insert the resulting values into a new list. In other words, this list comprehension will create a new list that contains the square of each of the even elements of the original list.

If we were to write this using a for statement, it would look like this:

``````>>> lst = []
>>> for i in [1, 2, 3, 4]:
...     if i % 2 == 0:
...         lst += [i**2]
>>> lst
[4, 16]``````

Note: The `if` clause in a list comprehension is optional. For example, you can just say:

``````>>> [i**2 for i in [1, 2, 3, 4]]
[1, 4, 9, 16]``````

# Required Questions

### Q1: Skip Add

Write a function `skip_add` that takes a single argument `n` and computes the sum of every other integer between `0` and `n`. Assume `n` is non-negative.

``````def skip_add(n):
""" Takes a number x and returns x + x-2 + x-4 + x-6 + ... + 0.

>>> skip_add(5)  # 5 + 3 + 1 + 0
9
>>> skip_add(10) # 10 + 8 + 6 + 4 + 2 + 0
30
>>> # Do not use while/for loops!
>>> from construct_check import check
>>> check('lab03.py', 'skip_add',
...       ['While', 'For'])
True
"""
"*** YOUR CODE HERE ***"
if n <= 0:
return 0
return n + skip_add(n - 2)``````

Use Ok to test your code:

``python3 ok -q skip_add``

### Q2: Hailstone

Recall the `hailstone` function from homework 1. First, 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.

Hint: When taking the recursive leap of faith, consider both the return value and side effect of this function.

``````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
>>> # Do not use while/for loops!
>>> from construct_check import check
>>> check('lab03.py', 'hailstone',
...       ['While', 'For'])
True
"""
"*** 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)

Video walkthrough: https://youtu.be/E4sagKZ2qWc``````

Use Ok to test your code:

``python3 ok -q hailstone``

### Q3: If This Not That

Define `if_this_not_that`, which takes a list of integers `i_list` and an integer `this`. For each element in `i_list`, if the element is larger than `this`, then print the element. Otherwise, print `"that"`.

``````def if_this_not_that(i_list, this):
"""Define a function which takes a list of integers `i_list` and an integer
`this`. For each element in `i_list`, print the element if it is larger
than `this`; otherwise, print the word "that".

>>> original_list = [1, 2, 3, 4, 5]
>>> if_this_not_that(original_list, 3)
that
that
that
4
5
"""
"*** YOUR CODE HERE ***"
for elem in i_list:
if elem <= this:
print("that")
else:
print(elem)

# List comprehension version
def if_this_not_that(i_list, this):
[print(i) if i > this else print('that') for i in i_list]
``````

Use Ok to test your code:

``python3 ok -q if_this_not_that``

# Optional Questions

Note: The following questions are in lab03_extra.py.

## More Recursion Practice

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

Video walkthrough: https://youtu.be/E4Rhz2KNH4w``````

Use Ok to test your code:

``python3 ok -q is_prime``

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

Video walkthrough: https://youtu.be/yBhhfBObNxs``````

Use Ok to test your code:

``python3 ok -q gcd``

### Q6: 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 pair of digits within `n` that sums 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. Note that a digit can be part of more than one ten-pair.

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

## More Lists Practice

### Q7: Factors List

Write `factors_list`, which takes a number `n` and returns a list of its factors in ascending order.

``````def factors_list(n):
"""Return a list containing all the numbers that divide `n` evenly, except
for the number itself. Make sure the list is in ascending order.

>>> factors_list(6)
[1, 2, 3]
>>> factors_list(8)
[1, 2, 4]
>>> factors_list(28)
[1, 2, 4, 7, 14]
"""
all_factors = []
"*** YOUR CODE HERE ***"
x = 1
while x < n:
if n % x == 0:
all_factors += [x]
x += 1

return all_factors``````

Use Ok to test your code:

``python3 ok -q factors_list``