Homework 3 Solutions
Solution Files
You can find solutions for all questions in hw03.py.
Required Questions
Getting Started Videos
These videos may provide some helpful direction for tackling the coding problems on this assignment.
To see these videos, you should be logged into your berkeley.edu email.
Higher-Order Functions
Q1: Product
Write a function called product that returns the product of the first n terms of a sequence.
Specifically, product takes in an integer n and term, a single-argument function that determines a sequence.
(That is, term(i) gives the ith term of the sequence.)
product(n, term) should return term(1) * ... * term(n).
def product(n, term):
"""Return the product of the first n terms in a sequence.
n: a positive integer
term: a function that takes an index as input and produces a term
>>> product(3, identity) # 1 * 2 * 3
6
>>> product(5, identity) # 1 * 2 * 3 * 4 * 5
120
>>> product(3, square) # 1^2 * 2^2 * 3^2
36
>>> product(5, square) # 1^2 * 2^2 * 3^2 * 4^2 * 5^2
14400
>>> product(3, increment) # (1+1) * (2+1) * (3+1)
24
>>> product(3, triple) # 1*3 * 2*3 * 3*3
162
"""
prod, k = 1, 1
while k <= n:
prod, k = term(k) * prod, k + 1
return prod
Use Ok to test your code:
python3 ok -q product
The prod variable is used to keep track of the product so far. We
start with prod = 1 since we will be multiplying, and anything multiplied
by 1 is itself. We then initialize the counter variable k to use in the while
loop to ensures that we get through all values 1 through k.
Q2: Funception
Implement funception, which takes in a function func1
and a number begin and returns a function func2. func2
should take a single argument, end, and apply func1 on
all the numbers from begin (inclusive) up
to end (exclusive) and return the product.
If begin is greater than or equal to end, the range of numbers
is invalid, and func2 should return 1.
Note 1: The function returned by
funceptionmerges terms over the range[begin, end).
Note 2: If you attempt to modify
begininfunc2, you will get anUnboundLocalError. You're not allowed to rebind variables defined outside of our current frame! However, you can still access the value ofbegininfunc2and set it equal to a new variable. An example is shown below:
Unbound Local Error:
>>> def f():
... x = 5
... def g():
... x += 1 # UnboundLocalError: cannot modify variable X outside current frame
... print(x)
... return g
...
>>> h = f()
>>> h()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in g
UnboundLocalError: local variable 'x' referenced before assignment
No Error:
>>> def f():
... x = 5
... def g():
... i = x # No Error: accesses value X from parent frame, binds it to I in the current frame
... i += 1
... print(i)
... return g
...
>>> h = f()
>>> h()
6
def funception(func1, begin):
""" Takes in a function (func1) and a begin value.
Returns a function (func2) that will find the product of
func1 applied to the range of numbers from
begin (inclusive) to end (exclusive)
>>> def increment(num):
... return num + 1
>>> def double(num):
... return num * 2
>>> g1 = funception(increment, 0)
>>> g1(3) # increment(0) * increment(1) * increment(2) = 1 * 2 * 3 = 6
6
>>> g1(0) # Returns 1 because begin >= end
1
>>> g1(-1) # Returns 1 because begin >= end
1
>>> g2 = funception(double, 1)
>>> g2(3) # double(1) * double(2) = 2 * 4 = 8
8
>>> g2(4) # double(1) * double(2) * double(3) = 2 * 4 * 6 = 48
48
>>> g3 = funception(increment, -3)
>>> g3(-1) # increment(-3) * increment(-2) = -2 * -1 = 2
2
"""
def func2(end):
i = begin
product = 1
while i < end:
product *= func1(i)
i += 1
return product
return func2
Use Ok to test your code:
python3 ok -q funception
Q3: Make Repeater
Implement the function make_repeater which takes a one-argument function f
and a positive integer n. It returns a one-argument function so that
make_repeater(f, n)(x) returns the value of f(f(...f(x)...)), in which f is
applied n times to x. For example, make_repeater(square, 3)(5) squares 5
three times and returns 390625, just like square(square(square(5))).
def make_repeater(f, n):
"""Returns the function that computes the nth application of f.
>>> add_three = make_repeater(increment, 3)
>>> add_three(5)
8
>>> make_repeater(triple, 5)(1) # 3 * (3 * (3 * (3 * (3 * 1))))
243
>>> make_repeater(square, 2)(5) # square(square(5))
625
>>> make_repeater(square, 3)(5) # square(square(square(5)))
390625
"""
def repeater(x):
k = 0
while k < n:
x, k = f(x), k + 1
return x
return repeater
Use Ok to test your code:
python3 ok -q make_repeater
There are many correct ways to implement make_repeater. This solution
repeatedly applies h.
Check Your Score Locally
You can locally check your score on each question of this assignment by running
python3 ok --score
This does NOT submit the assignment! When you are satisfied with your score, submit the assignment to Gradescope to receive credit for it.
Submit Assignment
Submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment. Lab 00 has detailed instructions.