Homework 7
Due by 11:59pm on Sunday, 11/13
Instructions
Download hw07.zip. Inside the archive, you will find a file called hw07.py, along with a copy of the OK autograder. Upload the zip file to Jupyter to complete the assignment. See Lab 0 for instructions on using Jupyter to complete assignments.
Submission: When you are done, submit the assignment by uploading the .py file to okpy.org. You may submit more than once before the deadline; only the final submission will be scored. See Lab 0 for instructions on submitting assignments.
Readings: This homework relies on following references:
Question 1: Leaves
Write a function leaves that returns a list of all the entries of the leaf
nodes of a Tree.
def leaves(t):
"""Returns a list of all the entries of the leaf nodes of the Tree t.
>>> leaves(Tree(1))
[1]
>>> leaves(Tree(1, [Tree(2, [Tree(3)]), Tree(4)]))
[3, 4]
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python ok -q leaves --local
Question 2: Person
Modify the following Person class to add a repeat method, which
repeats the last thing said. See the doctests for an example of its
use.
Hint: you will have to modify other methods as well, not just the
repeatmethod.
class Person(object):
"""Person class.
>>> steven = Person("Steven")
>>> steven.repeat() # starts at whatever value you'd like
'I squirreled it away before it could catch on fire.'
>>> steven.say("Hello")
'Hello'
>>> steven.repeat()
'Hello'
>>> steven.greet()
'Hello, my name is Steven'
>>> steven.repeat()
'Hello, my name is Steven'
>>> steven.ask("preserve abstraction barriers")
'Would you please preserve abstraction barriers'
>>> steven.repeat()
'Would you please preserve abstraction barriers'
"""
def __init__(self, name):
self.name = name
"*** YOUR CODE HERE ***"
def say(self, stuff):
"*** YOUR CODE HERE ***"
return stuff
def ask(self, stuff):
return self.say("Would you please " + stuff)
def greet(self):
return self.say("Hello, my name is " + self.name)
def repeat(self):
"*** YOUR CODE HERE ***"
Use OK to test your code:
python ok -q Person --local
Question 3: Keyboard
We'd like to create a Keyboard class that takes in an arbitrary
number of Buttons and stores these Buttons in a dictionary. The
keys in the dictionary will be ints that represent the postition on the
Keyboard, and the values will be the respective Button. Fill out
the methods in the Keyboard class according to each description,
using the doctests as a reference for the behavior of a Keyboard.
class Keyboard:
"""A Keyboard takes in an arbitrary amount of buttons, and has a
dictionary of positions as keys, and values as Buttons.
>>> b1 = Button(0, "H")
>>> b2 = Button(1, "I")
>>> k = Keyboard(b1, b2)
>>> k.buttons[0].key
'H'
>>> k.press(1)
'I'
>>> k.typing([0, 1])
'HI'
>>> k.typing([1, 0])
'IH'
>>> b1.pressed
2
>>> b2.pressed
3
"""
def __init__(self, *args):
"*** YOUR CODE HERE ***"
def press(self, info):
"""Takes in a position of the button pressed, and
returns that button's output"""
"*** YOUR CODE HERE ***"
def typing(self, typing_input):
"""Takes in a list of positions of buttons pressed, and
returns the total output"""
"*** YOUR CODE HERE ***"
class Button:
def __init__(self, pos, key):
self.pos = pos
self.key = key
self.pressed = 0
Use OK to test your code:
python ok -q Keyboard --local
Question 4: Vending Machine
Create a class called VendingMachine that represents a vending
machine for some product. A VendingMachine object returns strings
describing its interactions. See the doctest below for examples:
class VendingMachine:
"""A vending machine that vends some product for some price.
>>> v = VendingMachine('candy', 10)
>>> v.vend()
'Machine is out of stock.'
>>> v.restock(2)
'Current candy stock: 2'
>>> v.vend()
'You must deposit $10 more.'
>>> v.deposit(7)
'Current balance: $7'
>>> v.vend()
'You must deposit $3 more.'
>>> v.deposit(5)
'Current balance: $12'
>>> v.vend()
'Here is your candy and $2 change.'
>>> v.deposit(10)
'Current balance: $10'
>>> v.vend()
'Here is your candy.'
>>> v.deposit(15)
'Machine is out of stock. Here is your $15.'
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python ok -q VendingMachine --local
Question 5
Create a class called MissManners that promotes politeness among our
objects. A 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.
Hint: Your implementation will need to use the *args notation that
allows functions to take a flexible number of arguments.
Hint: Use getattr and hasattr to manipulate attributes using strings.
class MissManners:
"""A container class that only forward 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.'
>>> really_fussy = MissManners(m)
>>> really_fussy.ask('deposit', 10)
'You must learn to say please first.'
>>> really_fussy.ask('please deposit', 10)
'Thanks for asking, but I know not how to deposit.'
>>> really_fussy.ask('please please deposit', 10)
'Thanks for asking, but I know not how to please deposit.'
>>> really_fussy.ask('please ask', 'please deposit', 10)
'Current balance: $10'
"""
"*** YOUR CODE HERE ***"
Use OK to test your code:
python ok -q MissManners --local