Extra Homework 1
Due by 11:59pm on Wednesday, 2/14/2018
Instructions
Download extra01.zip. Inside the archive, you will find a file called extra01.py, 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.
Using Ok
The ok
program helps you test your code and track your progress.
The first time you run the autograder, you will be asked to log in with your
@berkeley.edu account using your web browser. Please do so. Each time you run
ok, it will back up your work and progress on our servers.
You can run all the doctests with the following command:
python3 ok
To test a specific question, use the -q
option with the
name of the function:
python3 ok -q <function>
By default, only tests that fail will appear. If you
want to see how you did on all tests, you can use the -v
option:
python3 ok -v
If you do not want to send your progress to our server or you have any
problems logging in, add the --local
flag to block all
communication:
python3 ok --local
When you are ready to submit, run ok
with the
--submit
option:
python3 ok --submit
Readings: You might find the following references useful:
Newton's Method
Q1
Implement intersect
, which takes two functions f
and g
and their
derivatives df
and dg
. It returns an intersection point x
, at which
f(x)
is equal to g(x)
.
def intersect(f, df, g, dg):
"""Return where f with derivative df intersects g with derivative dg.
>>> parabola, line = lambda x: x*x - 2, lambda x: x + 10
>>> dp, dl = lambda x: 2*x, lambda x: 1
>>> intersect(parabola, dp, line, dl)
4.0
"""
"*** YOUR CODE HERE ***"
Hog Decision Making Algorithm
Q2
Finish the implementation of roll_at_least
, which takes two integers score
and n
, along with a dice function dice
(one that returns a probability for a given input, not one that simulates a dice), and returns the probability of getting at least score
when rolling n
dice. This function should follow the general pattern from lecture, where we split it up into two separate cases. Memoization is also provided.
@memoize
def roll_at_least(score, n, dice=six_sided):
"""
>>> "%.6f" % roll_at_least(1, 1) # rounding to avoid floating point errors
'1.000000'
>>> "%.6f" % roll_at_least(2, 2)
'0.694444'
>>> "%.6f" % roll_at_least(20, 3)
'0.000000'
>>> "%.6f" % roll_at_least(20, 4)
'0.054012'
>>> "%.6f" % roll_at_least(20, 9)
'0.193806'
>>> "%.6f" % roll_at_least(7, 2)
'0.527778'
>>> "%.6f" % roll_at_least(7, 4)
'0.482253'
>>> "%.6f" % roll_at_least(14, 4)
'0.388117'
>>> "%.6f" % roll_at_least(14, 9)
'0.193807'
>>> "%.6f" % roll_at_least(14, 14)
'0.077887'
"""
return roll_at_least_ones(score, n, dice) + roll_at_least_no_ones(score, n, dice)
@memoize
def roll_at_least_ones(total, n, dice):
"*** YOUR CODE HERE ***"
@memoize
def roll_at_least_no_ones(total, n, dice):
"*** YOUR CODE HERE ***"