"""CS 61A HW 10 Name: Login: TA: Section: """ #### Core Questions #### Q0 """Draw environment diagrams for each set of doctests below. Turn these in on paper. """ #### Q1 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 """ def account(msg, *args): "*** YOUR CODE HERE ***" return account #### Q2 """Draw the environment diagram for the following doctest. >>> x = [1, 2, 3] >>> def foobar(y): ... y[1] = x ... >>> foobar(x) >>> z = x[1] >>> z[0] = 5 >>> x[0] 5 >>> x[1] = 33 >>> def map(fn, lst): ... for i in range(len(lst)): ... lst[i] = fn(lst[i]) ... >>> map(lambda x: x + z[2], x) >>> x [8, 36, 6] """ #### Reinforcement Questions #### Q3 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 """ "*** YOUR CODE HERE ***" #### Q4 def make_miss_manners(dispatch): """Makes a new dispatch function that uses the old dispatch function whenever you give it the message "please " >>> 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 """ "*** YOUR CODE HERE ***" #### Extra for Experts """There is no Extra for Experts in this homework. Go study for the exam."""