# Lab 4: Recursion and Lists lab04.zip

Due by 11:59pm on Friday, September 27.

## Starter Files

Download lab04.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.

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

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

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

For example, `5! = 5 * 4 * 3 * 2 * 1 = 120`

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 comma 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, ]]``````

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
6
>>> lst
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)
[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 = 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

## Recursion

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.

``````this_file = __file__

""" Takes a number n and returns n + n-2 + n-4 + n-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
>>> # ban iteration
...       ['While', 'For'])
True
"""
if n <= 0:
return 0
return n + skip_add(n - 2)``````

Use Ok to test your code:

``python3 ok -q skip_add``

### Q2: Summation

Now, 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 of the results.

``````def summation(n, term):

"""Return the sum of the first n terms in the sequence defined by term.
Implement using recursion!

>>> summation(5, lambda x: x * x * x) # 1^3 + 2^3 + 3^3 + 4^3 + 5^3
225
>>> summation(9, lambda x: x + 1) # 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
54
>>> summation(5, lambda x: 2**x) # 2^1 + 2^2 + 2^3 + 2^4 + 2^5
62
>>> # Do not use while/for loops!
>>> from construct_check import check
>>> # ban iteration
>>> check(this_file, 'summation',
...       ['While', 'For'])
True
"""
assert n >= 1
if n == 1:
return term(n)
else:
return term(n) + summation(n - 1, term)
# Base case: only one item to sum, so we return that item.
# Recursive call: returns the result of summing the numbers up to n-1 using
# term. All that's missing is term applied to the current value n.``````

Use Ok to test your code:

``python3 ok -q summation``

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

## Lists Practice

### Q4: List Indexing

Use Ok to test your knowledge with the following "List Indexing" questions:

``python3 ok -q list-indexing -u``

For each of the following lists, what is the list indexing expression that evaluates to `7`? For example, if `x = `, then the answer would be `x`. You can use the interpreter or Python Tutor to experiment with your answers.

``````>>> x = [1, 3, [5, 7], 9]
______x
>>> x = [[3, [5, 7], 9]]
______x``````

What would Python display? If you get stuck, try it out in the Python interpreter!

``````>>> lst = [3, 2, 7, [84, 83, 82]]
>>> lst
______Error
>>> lst
______84``````

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

``````def couple(s1, s2):
"""Return a list that contains lists with i-th elements of two sequences
coupled together.
>>> s1 = [1, 2, 3]
>>> s2 = [4, 5, 6]
>>> couple(s1, s2)
[[1, 4], [2, 5], [3, 6]]
>>> s3 = ['c', 6]
>>> s4 = ['s', '1']
>>> couple(s3, s4)
[['c', 's'], [6, '1']]
"""
assert len(s1) == len(s2)
return [[s1[i], s2[i]] for i in range(0, len(s1))]``````

Use Ok to test your code:

``python3 ok -q couple``

### Q6: Enumerate

Implement `enumerate`, which pairs the elements of a sequence with their indices, offset by a starting value. `enumerate` takes a sequence `s` and a starting value `start`. It returns a list of pairs, in whichthe i-th element is `i + start` paired with the i-th element of `s`. For example:

``````>>> enumerate(['maps', 21, 47], start=1)
>>> [[1, 'maps'], [2, 21], [3, 47]]``````

Hint: Consider using `couple`!

Hint 2: You may find the built in range function helpful.

``````def enumerate(s, start=0):
"""Returns a list of lists, where the i-th list contains i+start and
the i-th element of s.
>>> enumerate([6, 1, 'a'])
[[0, 6], [1, 1], [2, 'a']]
>>> enumerate('five', 5)
[[5, 'f'], [6, 'i'], [7, 'v'], [8, 'e']]
"""
return couple(range(start, start+len(s)), s)``````

Use Ok to test your code:

``python3 ok -q enumerate``

# Optional Questions

### Q7: 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
"""
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``

### Q8: Squares only

Implement the function `squares`, which takes in a list of positive integers. It returns a list that contains the square roots of the elements of the original list that are perfect squares. Try using a list comprehension.

You may find the `round` function useful.

``````>>> round(10.5)
10
>>> round(10.51)
11``````
``````def squares(s):
"""Returns a new list containing square roots of the elements of the
original list that are perfect squares.

>>> seq = [8, 49, 8, 9, 2, 1, 100, 102]
>>> squares(seq)
[7, 3, 1, 10]
>>> seq = [500, 30]
>>> squares(seq)
[]
"""
return [round(n ** 0.5) for n in s if n == round(n ** 0.5) ** 2]``````

It might be helpful to construct a skeleton list comprehension to begin with:

``[sqrt(x) for x in s if is_perfect_square(x)]``

This is great, but it requires that we have an `is_perfect_square` function. How might we check if something is a perfect square?

• If the square root of a number is a whole number, then it is a perfect square. For example, `sqrt(61) = 7.81024...` (not a perfect square) and `sqrt(49) = 7` (perfect square).
• Once we obtain the square root of the number, we just need to check if something is a whole number. The `is_perfect_square` function might look like:

``````def is_perfect_square(x):
return is_whole(sqrt(x))``````
• One last piece of the puzzle: to check if a number is whole, we just need to see if it has a decimal or not. The way we've chosen to do it in the solution is to compare the original number to the round version (thus removing all decimals), but a technique employing floor division (`//`) or something else entirely could work too.

We've written all these helper functions to solve this problem, but they are actually all very short. Therefore, we can just copy the body of each into the original list comprehension, arriving at the solution we finally present.

Video walkthrough: https://youtu.be/YwLFB9paET0

Use Ok to test your code:

``python3 ok -q squares``

### Q9: Key of Min Value

The built-in `min` function takes a collection of elements (such as a list or a dictionary) and returns the collection's smallest element. The `min` function also takes in an optional keyword argument called `key`, which must be a one-argument function. The `key` function is called on each element of the collection, and the return values are used for comparison. For example:

``````>>> min([-1, 0, 1])                    # no key argument; return smallest input
-1
>>> min([-1, 0, 1], key=lambda x: x*x) # return input with the smallest square
0``````

Implement `key_of_min_value`, which takes in a dictionary `d` and returns the key that corresponds to the minimum value in `d`. This behavior differs from just calling `min` on a dictionary, which would return the smallest key. Make sure your solution is only one line and uses the `min` function.

``````def key_of_min_value(d):
"""Returns the key in a dict d that corresponds to the minimum value of d.
>>> letters = {'a': 6, 'b': 5, 'c': 4, 'd': 5}
>>> min(letters)
'a'
>>> key_of_min_value(letters)
'c'
"""
``python3 ok -q key_of_min_value``