Homework 3
Due at 11:59:59 pm on Sunday, 2/23/2020.
Instructions
Download hw03.zip. Inside the archive, you will find starter files for the questions in this homework, along with a copy of the OK autograder.
Submission: When you are done, submit with python3 ok --submit
. You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy.org. See this article for more instructions on okpy and submitting assignments.
Readings: This homework relies on following references:
Questions
Question 1: Harmonic Mean
Implement harmonic_mean
, which returns the harmonic mean of two positive numbers
x
and y
. The harmonic mean of 2 numbers is 2 divided by the sum of the
reciprocals of the numbers. (The reciprocal of x
is 1/x
.)
def harmonic_mean(x, y):
"""Return the harmonic mean of x and y.
>>> harmonic_mean(2, 6)
3.0
>>> harmonic_mean(1, 1)
1.0
>>> harmonic_mean(2.5, 7.5)
3.75
>>> harmonic_mean(4, 12)
6.0
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q harmonic_mean
Question 2: Two of three
Write a function that takes three positive numbers and returns the sum of the squares of the two largest numbers. Use only a single expression for the body of the function.
def two_of_three(a, b, c):
"""Return x*x + y*y, where x and y are the two largest members of the
positive numbers a, b, and c.
>>> two_of_three(1, 2, 3)
13
>>> two_of_three(5, 3, 1)
34
>>> two_of_three(10, 2, 8)
164
>>> two_of_three(5, 5, 5)
50
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q two_of_three
Question 3: Nonzero
Write a function that takes in a list and returns the first nonzero entry.
def nonzero(lst):
""" Returns the first nonzero element of a list
>>> nonzero([1, 2, 3])
1
>>> nonzero([0, 1, 2])
1
>>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6])
5
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q nonzero
Question 4: Contains N
Write a function that takes in a list and a number, and returns whether or not the list contains the value n.
def has_n(lst, n):
""" Returns whether or not a list contains the value n.
>>> has_n([1, 2, 2], 2)
True
>>> has_n([0, 1, 2], 3)
False
>>> has_n([], 5)
False
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q has_n
Question 5: Fibonacci
The Fibonacci sequence is a famous sequence in mathematics. The first element in the sequence is 0 and the second element is 1. The nth element is defined as Fn = Fn-1 + Fn-2.
Implement the fib
function, which takes an integer n
and returns
the n
th Fibonacci number. Use a while
loop in your solution.
def fib(n):
"""Returns the nth Fibonacci number.
>>> fib(0)
0
>>> fib(1)
1
>>> fib(2)
1
>>> fib(3)
2
>>> fib(4)
3
>>> fib(5)
5
>>> fib(6)
8
>>> fib(100)
354224848179261915075
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q fib
Question 6: Adding matrices
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]]
>>> add_matrices(matrix1, matrix2)
[[-2, 3], [3, 2]]
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q add_matrices
Question 7: Coordinates
Implement a function coords
, which takes a function, a sequence, and
an upper and lower bound on output of the function. coords
then
returns a list of x, y coordinate pairs (lists) such that:
- Each pair contains
[x, fn(x)]
- The x coordinates are the elements in the sequence
- Only pairs whose y coordinate is within the upper and lower bounds (inclusive)
See the doctests for examples.
One other thing: your answer can only be one line long. You should make use of list comprehensions!
def coords(fn, seq, lower, upper):
"""
>>> seq = [-4, -2, 0, 1, 3]
>>> def fn(x):
... return x**2
>>> coords(fn, seq, 1, 9)
[[-2, 4], [1, 1], [3, 9]]
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q coords