# Homework 7 hw07.zip

Due by 11:59pm on Tuesday, 10/10

## Instructions

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 okpy.org. 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:

### Q1: Digits

Implement digits, which returns a linked list containing the digits of a non-negative integer n.

def digits(n):
"""Return the digits of n as a linked list.

True
>>> digits(543)
"""
while n > 0:
n, last = n // 10, n % 10
return s

Use Ok to test your code:

python3 ok -q digits

### Q2: Next Fibonacci Object

Implement the next method of the Fib class. For this class, the value attribute is a Fibonacci number. The next method returns a Fib instance whose value is the next Fibonacci number. The next method should take only constant time.

Hint: Assign value and previous attributes within next.

class Fib():
"""A Fibonacci number.

>>> start = Fib()
>>> start
0
>>> start.next()
1
>>> start.next().next()
1
>>> start.next().next().next()
2
>>> start.next().next().next().next()
3
>>> start.next().next().next().next().next()
5
>>> start.next().next().next().next().next().next()
8
>>> start.next().next().next().next().next().next() # Ensure start isn't changed
8
"""

def __init__(self, value=0):
self.value = value

def next(self):

def __repr__(self):
return str(self.value)

Use Ok to test your code:

python3 ok -q Fib

### Q3: 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.deposit(15)
'Machine is out of stock. Here is your \$15.'
>>> 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()
>>> v.deposit(15)
'Machine is out of stock. Here is your \$15.'

>>> w = VendingMachine('soda', 2)
>>> w.restock(3)
'Current soda stock: 3'
>>> w.restock(3)
'Current soda stock: 6'
>>> w.deposit(2)
'Current balance: \$2'
>>> w.vend()
"""

You may find Python string formatting syntax useful. A quick example:

>>> ten, twenty, thirty = 10, 'twenty', [30]
>>> '{0} plus {1} is {2}'.format(ten, twenty, thirty)
'10 plus twenty is [30]'

Use Ok to test your code:

python3 ok -q VendingMachine

### Q4: Miss Manners

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.

We can also compose multiple instances of MissManners objects upon each other (see double_fussy in doctests). A multilevel MissManners object must be politely requested to ask its own MissManners object. This continues until we reach the bottom level MissManners object.

Hint: Use getattr and hasattr to access methods using strings.

Hint: Your implementation will need to use the *args notation that allows functions to take a flexible number of arguments.

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)
'You must learn to say please first.'
'You must deposit \$10 more.'
'Current balance: \$20'
'You must learn to say please first.'
'Thanks for asking, but I know not how to hand over a teaspoon.'
'Here is your teaspoon and \$10 change.'

>>> double_fussy = MissManners(m) # Composed MissManners objects
'You must learn to say please first.'
'Thanks for asking, but I know not how to deposit.'
'Current balance: \$10'
"""
def __init__(self, obj):
self.obj = obj