Due by 11:59pm on Tuesday, 3/13
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
See Lab 0
for more instructions on submitting assignments.
Using Ok: If you have any questions about using Ok, please refer to this guide.
Readings: You might find the following references useful:
digits, which returns a linked list containing the digits of a
def digits(n): """Return the digits of n as a linked list. >>> digits(0) is Link.empty True >>> digits(543) Link(5, Link(4, Link(3))) """ s = Link.empty while n > 0: n, last = n // 10, n % 10 "*** YOUR CODE HERE ***" return s
Use Ok to test your code:
python3 ok -q digits
Q2: Miss Manners
Create a class called
MissManners that promotes politeness among our
MissManners object takes another object on construction.
It has one method, called
ask. It responds by calling methods on the
object it contains, but only if the caller said please first.
We can also compose multiple instances of
MissManners objects upon
each other (see
double_fussy in doctests). A multilevel
must be politely requested to ask its own
MissManners object. This continues
until we reach the bottom level
Hint: Your implementation will need to use the
*argsnotation that allows functions to take a flexible number of arguments. If you need a refresher, take a look at your implementation of
make_averagedin the Hog project.
class MissManners: """A container class that only forwards messages that say please. >>> v = VendingMachine('teaspoon', 10) >>> v.restock(2) 'Current teaspoon stock: 2' >>> m = MissManners(v) >>> m.ask('vend') 'You must learn to say please first.' >>> m.ask('please vend') 'You must deposit $10 more.' >>> m.ask('please deposit', 20) 'Current balance: $20' >>> m.ask('now will you vend?') 'You must learn to say please first.' >>> m.ask('please hand over a teaspoon') 'Thanks for asking, but I know not how to hand over a teaspoon.' >>> m.ask('please vend') 'Here is your teaspoon and $10 change.' >>> double_fussy = MissManners(m) # Composed MissManners objects >>> double_fussy.ask('deposit', 10) 'You must learn to say please first.' >>> double_fussy.ask('please deposit', 10) 'Thanks for asking, but I know not how to deposit.' >>> double_fussy.ask('please please deposit', 10) 'Thanks for asking, but I know not how to please deposit.' >>> double_fussy.ask('please ask', 'please deposit', 10) 'Current balance: $10' """ def __init__(self, obj): self.obj = obj def ask(self, message, *args): magic_word = 'please ' if not message.startswith(magic_word): return 'You must learn to say please first.' "*** YOUR CODE HERE ***"
Use Ok to test your code:
python3 ok -q MissManners