Homework 4
Due at 11:59:59 pm on Thursday, February 24.
Instructions
Download hw04.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.
Readings: This homework relies on following references:
Lambdas
Question 1: Make your own lambdas
For each of the following expressions, write functions f1
, f2
,
f3
, and f4
such that the evaluation of each expression
succeeds, without causing an error. Be sure to use lambdas in your
function definition instead of nested def
statements. Each function
should have a one line solution.
def f1():
"""
>>> f1()
3
"""
"*** YOUR CODE HERE ***"
def f2():
"""
>>> f2()()
3
"""
"*** YOUR CODE HERE ***"
def f3():
"""
>>> f3()(3)
3
"""
"*** YOUR CODE HERE ***"
def f4():
"""
>>> f4()()(3)()
3
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q f1
python3 ok -q f2
python3 ok -q f3
python3 ok -q f4
Question 2: Higher Order Lambdas
Return a lambda function that takes in a multiplier and returns a lambda function that given an input will return the input multiplied by the multiplier.
def higher_order_lambdas():
"""
Return a lambda function that takes in a multiplier and returns a lambda function that given an input will
return the input multiplied by the multiplier
>>> hol = higher_order_lambdas()
>>> doubles = hol(2)
>>> doubles(3)
6
>>> hol = higher_order_lambdas()
>>> triples = hol(3)
>>> triples(4)
12
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q higher_order_lambdas
Question 3: Lambdas and Currying
We can transform multiple-argument functions into a chain of single-argument, higher order functions by taking advantage of lambda expressions. This is useful when dealing with functions that take only single-argument functions. We will see some examples of these later on.
Write a function lambda_curry2
that will curry any two argument
function using lambdas. See the doctest if you're not sure what this
means.
def lambda_curry2(fn):
"""
Returns a Curried version of a two argument function func.
>>> from operator import add
>>> x = lambda_curry2(add)
>>> y = x(3)
>>> y(5)
8
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q lambda_curry2
Dictionaries
Question 4: Replace All
Given a dictionary d
, return a new dictionary where all occurences of x
as a value (not a key) is replaced with y
.
def replace_all(d, x, y):
"""
>>> d = {'foo': 2, 'bar': 3, 'garply': 3, 'xyzzy': 99}
>>> e = replace_all(d, 3, 'poof')
>>> e == {'foo': 2, 'bar': 'poof', 'garply': 'poof', 'xyzzy': 99}
True
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q replace_all
Question 5: 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
Submit
Make sure to submit this assignment by running:
python3 ok --submit