Due by 11:59 PM on (that is, the end of) Tuesday, 7/24
This homework must be submitted online. We do not require a paper copy. If you would like a reader to give you written feedback on your homework, submit a paper copy to the homework box.
To turn in the electronic copy, submit all of your answers in a file named hw10.py. Follow the instructions here to submit the electronic copy.
If you would like, you can use the template file
To copy this file to your lab account you can run the command:
cp ~cs61a/lib/hw/hw10/hw10.py .
to copy it into your current directory.
This homework has a paper submission component. You will be drawing environment diagrams for each question and we do not want you to turn in images using the submit program.
In homeworks, we have three different categories of questions:
It is our hope that these categories will help guide you in deciding how to divide your time when working on the homeworks.
Note: For each question below, draw an environment diagram of the doctests provided and turn these diagrams on paper into the homework box. Yes, we realize some of them are complicated, which is why the homework is short.
Q1. We will simulate a bank account data type using dispatch functions! Complete the definition of make_account, which creates a bank account, itself implemented as a function.
def make_account(balance): """Make a bank account dispatch function. >>> tom_acct = make_account(100) >>> tom_acct('balance') 100 >>> tom_acct('deposit', 20) >>> tom_acct('balance') 120 >>> tom_acct('withdraw', 30) >>> tom_acct('balance') 90 """
Q2. Draw the environment diagram that results from running the doctest given in the template file for this question. Turn in this diagram with your paper submission. You do not need to write any code for this question.
>>> x = [1, 2, 3] >>> def foobar(y): ... y = x ... >>> foobar(x) >>> z = x >>> z = 5 >>> x 5 >>> x = 33 >>> def map(fn, lst): ... for i in range(len(lst)): ... lst[i] = fn(lst[i]) ... >>> map(lambda x: x + z, x) >>> x [8, 36, 6]
Q3. For this question, we will extend the idea of counters that we saw in lecture. Complete the definition of make_advanced_counter_maker, which creates a function that creates counters. These counters can not only update their personal count, but a globally shared count for all counters. They can also reset either count.
def make_advanced_counter_maker(): """Makes a function that makes counters that understands the messages "count", "global-count", "reset", and "global-reset". See the examples below: >>> make_counter = make_advanced_counter_maker() >>> tom_counter = make_counter() >>> tom_counter('count') 1 >>> tom_counter('count') 2 >>> tom_counter('global-count') 1 >>> jon_counter = make_counter() >>> jon_counter('global-count') 2 >>> jon_counter('count') 1 >>> jon_counter('reset') >>> jon_counter('count') 1 >>> tom_counter('count') 3 >>> jon_counter('global-count') 3 >>> jon_counter('global-reset') >>> tom_counter('global-count') 1 """
Q4. In homework 8, we saw a MissManners class, which promoted politeness among our objects. We will implement a similar MissManners data type using dispatch functions. It will take another dispatch function and produce a dispatch function, which requires the word please to appear before the message you would have given the original dispatch function.
def make_miss_manners(dispatch): """Makes a new dispatch function that uses the old dispatch function whenever you give it the message "please <action>" >>> def make_counter(): ... count = 0 ... def counter(msg): ... nonlocal count ... if msg == "count": ... count += 1 ... return count ... print("Sorry, I don't know how to " + msg) ... return counter ... >>> manners_counter = make_miss_manners(make_counter()) >>> manners_counter("count") You must say please! >>> manners_counter("please count") 1 >>> manners_counter("count") You must say please! >>> manners_counter("please count") 2 """
Extra for Experts
There are no questions for this section on this homework. Instead, study for the exam!