Due at 9:00pm on 03/11/2019.

## Starter Files

Download lab06.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. See this article for more instructions on okpy and submitting assignments.

• To receive full credit for this lab, all questions must be attempted.

When you are ready to submit, run `ok` with the `--submit` option:

python3 ok —submit

After submitting, `ok` will display a submission URL, with which you can view your submission on okpy.org.

## Questions

### Question 1: Has Seven

Write a function `has_seven` that takes a positive integer `n` and returns whether `n` contains the digit 7. Do not use any assignment statements - use recursion instead:

``````def has_seven(k):
"""Returns True if at least one of the digits of k is a 7, False otherwise.

>>> has_seven(3)
False
>>> has_seven(7)
True
>>> has_seven(2734)
True
>>> has_seven(2634)
False
>>> has_seven(734)
True
>>> has_seven(7777)
True
"""
if k % 10 == 7:
return True
elif k < 10:
return False
else:
return has_seven(k // 10)``````

Use OK to test your code:

``python3 ok -q has_seven``

### Question 2: Deep-Len

A list that contains one or more lists as elements is called a deep list. For example, `[1, [2, 3], 4]` is a deep list.

Write a function `deep_len` that takes a list and returns its deep length. See the doctests for the function's behavior.

Hint: you can check if something is a list by using the built-in `type` function. For example,

``````>>> type(3) == list
False
>>> type([1, 2, 3]) == list
True``````
``````def deep_len(lst):
"""Returns the deep length of the list.

>>> deep_len([1, 2, 3])     # normal list
3
>>> x = [1, [2, 3], 4]      # deep list
>>> deep_len(x)
4
>>> x = [[1, [1, 1]], 1, [1, 1]] # deep list
>>> deep_len(x)
6
"""
if not lst:
return 0
elif type(lst[0]) == list:
return deep_len(lst[0]) + deep_len(lst[1:])
else:
return 1 + deep_len(lst[1:])``````

Use OK to test your code:

``python3 ok -q deep_len``

### Question 3: Polynomial

A polynomial function is a function with coefficients, variables and constants. A polynomial function is said to be the nth degree polynomial if there is a term in the function with the variable to the nth degree. For example, a 4th degree polynomial must contain the term x^4 with some coefficient multiplied to it.

Complete the function `polynomial`, which takes in a degree and a list of coefficients. The function should output the corresponding polynomial function.

``````def polynomial(degree, coeffs):
"""
>>> fourth = polynomial(4, [3,6,2,1, 100])
>>> fourth(3)   # 3*(3**4) + 6*(3**3) + 2*(3**2) + 1*(3**1) + 100
526
>>> third = polynomial(3, [2, 0, 0, 0])
>>> third(4)   # 2*(4**3) + 0*(4**2) + 0*(4**1) + 0
128
"""

# Option 1
return lambda x: sum([coeffs[i]*(x ** (degree - i)) for i in range(degree + 1)])
# Option 2
def poly_func(x):
return sum([coeffs[i]*(x ** (degree - i)) for i in range(degree + 1)])
return poly_func``````

Use OK to test your code:

``python3 ok -q polynomial``

## Extra Questions

Extra questions are not worth extra credit and are entirely optional. They are designed to challenge you to think creatively!

To practice, write a function that adds two matrices together using list comprehensions. The function should take in two 2D lists of the same dimensions. Try to implement this in one line!

``````def add_matrices(x, y):
"""
>>> matrix1 = [[1, 3],
...            [2, 0]]
>>> matrix2 = [[-3, 0],
...            [1, 2]]
[[-2, 3], [3, 2]]
"""

return [[x[i][j] + y[i][j] for j in range(len(x[0]))]
for i in range(len(x))]``````

Use OK to test your code:

``python3 ok -q add_matrices``

### Question 5: Mul_by_num

Using higher order functions, complete the `mul_by_num` function. This function should take an argument and return a one argument function that multiplies any value passed to it by the original number.

``````def mul_by_num(num):
"""
Returns a function that takes one argument and returns num
times that argument.
>>> x = mul_by_num(5)
>>> y = mul_by_num(2)
>>> x(3)
15
>>> y(-4)
-8
"""

def f(x):
return num*x
return f``````

Use OK to test your code:

``python3 ok -q mul_by_num``

Write a function `skip_add` that takes a single argument `n` and computes the sum of every other integer between 0 and `n` starting from 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
"""
``python3 ok -q skip_add``