MixedJuiceVendor
Question 1: MixedJuiceVendor
Now fill in another class, MixedJuiceVendor, that also implements QueueVendingMachine. MixedJuiceVendor keeps an instance attribute self.fruits, a list of fruits, and creates and dispenses a drink made with the first two fruits in the inventory. It also keeps an inventory of self.cups, and each drink takes one cup to produce.
Write the dispense and collect_money methods. MixedJuiceVendor is a waste-free vending machine, so do not perform any operations that create new lists of fruits (i.e. use mutation!). See the doctests for details.
class MixedJuiceVendor(object):
""" A QueueVendingMachine that vends mixed juices.
>>> vendor = MixedJuiceVendor(['kiwi', 'mango', 'apple', 'guava'], 3)
>>> juice = vendor.dispense()
>>> juice
'kiwi-mango'
>>> vendor.collect_money(juice)
9
>>> juice = vendor.dispense()
>>> juice
'apple-guava'
>>> vendor.collect_money(juice)
19
>>> vendor.cups
1
>>> juice = vendor.dispense() # no fruits left!
>>> print(juice)
None
>>> vendor2 = MixedJuiceVendor(['guava', 'mango'], 0)
>>> juice = vendor2.dispense() # no cups!
>>> print(juice)
None
>>> vendor3 = MixedJuiceVendor(['lemon'], 1)
>>> juice = vendor3.dispense() # only one fruit!
>>> print(juice)
None
"""
def __init__(self, fruits, cups):
""" fruits is a list of fruits in the inventory. cups is the number of
cups left to put juice in.
"""
self.fruits = fruits
self.cups = cups
self.revenue = 0
def dispense(self):
""" Dispenses a mixed juice combining the first two fruits in the
fruit inventory. Juices can only be created if there are at least
two fruits left and there is at least one cup left.
"""
"*** YOUR CODE HERE ***"
def collect_money(self, item):
""" Each juice is priced based on how many letters make up its two
fruits.
"""
"*** YOUR CODE HERE ***"
Hint: use the pop(i) method on a list to remove and retrieve the element at index i.
>>> lst = [1, 2, 3, 4]
>>> lst.pop()
4
>>> lst.pop(1)
2
>>> lst
[1, 3]
A budding entrepreneur finds out about QueueVendingMachines, and, predicting that they’re the next big thing in our increasingly automated society, wants to invest in them. However, he simply wants to find the one machine that generates the most revenue, and buy 1000 of them. He hires you to write function total_revenue that takes in a QueueVendingMachine and return the total revenue it generates if it sells all its products.
def total_revenue(qvm):
""" Returns total possible revenue generated from qvm.
>>> juices = MixedJuiceVendor(['orange', 'mango', 'banana', 'guava'], 10)
>>> total_revenue(juices)
22
>>> more_juices = MixedJuiceVendor(['lemon', 'strawberry', 'grape', 'apple'], 20)
>>> total_revenue(more_juices)
25
"""
"*** YOUR CODE HERE ***"