Homework 5
Due at 11:59:59 pm on Friday 03/05/2021.
Instructions
Download hw05.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:
Question 1: Merge Dictionaries
Implement the function merge_dict
. The merge_dict
function merges two dictionaries with the same keys together.
def merge_dict(d1, d2):
"""Returns a dictionary with two dictionaries merged together. You can assume that the same keys appear in both dictionaries.
>>> data8 = {"midterms":1, "projects":3}
>>> data100 = {"midterms":2, "projects":3}
>>> combined_exams = merge_dict(data8, data100)
>>> combined_exams
{'midterms': 3, 'projects': 6}
>>> sunday_orders = {"pizza": 3, "hot dogs": 2, "fries": 5}
>>> monday_orders = {"pizza": 1, "hot dogs": 1, "fries": 8}
>>> combined_orders = merge_dict(sunday_orders, monday_orders)
>>> combined_orders
{'pizza': 4, 'hot dogs': 3, 'fries': 13}
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q merge_dict
Question 2: 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.
Hint: the staff solutions is one line and uses lambda + a list comprehension.
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
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q polynomial
Submit
Make sure to submit this assignment by running:
python3 ok --submit
Maps
This homework is shorter than normal homeworks in order to give you more time to work on the Maps project. Please get started on it early.