Starter Files

Download lab09.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the OK autograder.

Submission

By the end of this lab, you should have submitted the lab with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be graded. Check that you have successfully submitted your code on okpy.org. See this article for more instructions on okpy and submitting assignments.

OOP

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 ***"
if len(self.fruits) >= 2 and self.cups: self.cups -= 1 return self.fruits.pop(0) + "-" + self.fruits.pop(0)
def collect_money(self, item): """ Each juice is priced based on how many letters make up its two fruits. """
"*** YOUR CODE HERE ***"
self.revenue = self.revenue + len(item) - 1 return self.revenue

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]

Use OK to test your code:

python3 ok -q MixedJuiceVendor

Question 2: Total Revenue

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 a 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 ***"
total = 0 item = qvm.dispense() while item: total = qvm.collect_money(item) item = qvm.dispense() return total

Use OK to test your code:

python3 ok -q total_revenue

Optional

Question 3: Max Revenue

Now write a function max_revenue that takes in a list of QueueVendingMachines and returns the one with the highest total possible revenue. Use the function total_revenue that you just wrote.

def max_revenue(lst_of_qvm):
    """ Returns the QueueVendingMachine from lst_of_qvm that has the greatest 
        total possible revenue, or the first in the list if their total 
        revenues are equal.

    >>> juices = MixedJuiceVendor(['orange', 'mango', 'banana', 'guava'], 10)
    >>> more_juice = MixedJuiceVendor(['strawberry', 'grape', 'banana', 'apple'], 20)
    >>> best = max_revenue([juices, more_juice])
    >>> best is more_juice
    True
    """
"*** YOUR CODE HERE ***"
best, max_rev = None, -1 for qvm in lst_of_qvm: total_rev = total_revenue(qvm) if total_rev > max_rev: max_rev, best = total_rev, qvm return best #Alternate solution def max_revenue(lst_of_qvm): return max(lst_of_qvm, key=lambda qvm : total_revenue(qvm))

Use OK to test your code:

python3 ok -q max_revenue