Homework 4
Due at 11:59:59 pm on Thursday, September 29.
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.
f1
takes in nothing and returns 3. f2
takes in nothing and returns a function that takes in nothing and returns 3. f3
takes in nothing and returns a function that takes in 1 value and returns that same value.
f4
takes in nothing and returns a function. This function takes in nothing and returns another function. This next function takes in a value and returns yet another function. This final function takes in nothing and returns the value passed into the previous function (if this explanation is confusing, have a look at the doctest, and it might be more clear).
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 (the multiplier is a number) and returns a lambda function that will take in another number and will return the new 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 fn
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
by adding up their values for the corresponding keys and returning the resulting dictionary.
def merge_dict(d1, d2):
"""Returns a dictionary that is the result of two dictionaries being merged together.
Dictionaries are merged by adding up their values. 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