Instructions

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

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.

Readings: This homework relies on following references:

Problem 1: Diner

In this problem you will fill out three functions to complete the Group ADT and the Diner ADT. The goal is to organize how diners manage the groups that want to eat there and the tables where these groups sit.

It is important to take the time to read through the docstrings and the doctests. Additionally, make sure to not violate abstraction barriers for other ADTs, i.e. when implementing functions for the Diner ADT, do not violate abstraction barriers for the Group ADT, and vice versa.

# Diner ADT
def make_diner(name):
    """ Diners are represented by their name and the number of free tables they have."""
    return [name, 0]

def num_free_tables(diner):
    return diner[1]

def name(diner):
    return diner[0]
# You will implement add_table and serve which are part of the Diner ADT
# Group ADT
def make_group(name):
    """ Groups are represented by their name and their status."""
    return [name, 'waiting']

def name(group):
    return group[0]

def status(group):
    return group[1]

def set_eating_status(group):
    group[1] = 'eating'

# You will implement finish_eating which is part of the Group ADT    

Question 1: Add Table

Implement add_table which increases the diner's number of free tables by 1. add_table is part of the Diner ADT.

def add_table(diner):
    """
    >>> din = make_diner("Croads")
    >>> num_free_tables(din)
    0
    >>> add_table(din)
    >>> add_table(din)
    >>> num_free_tables(din)
    2
    """
    "*** YOUR CODE HERE ***"

Use OK to test your code:

python3 ok -q add_table

Question 2: Serve

Implement serve so that the diner uses one of its free tables to seat the group. If there are no free tables, return the string 'no available table'. If there are free tables, the group's status should be updated to 'eating' and the diner should have one less free table. serve is part of the Diner ADT.

def serve(diner, group):
    """
    >>> din = make_diner("Cafe 3")
    >>> add_table(din)
    >>> g1 = make_group("Vandana's Group")
    >>> g2 = make_group("Shreya's Group")
    >>> serve(din, g1)
    >>> status(g1)
    'eating'
    >>> num_free_tables(din)
    0
    >>> serve(din, g2)
    'no available table'
    >>> status(g2)
    'waiting'
    """
    "*** YOUR CODE HERE ***"

Use OK to test your code:

python3 ok -q serve

Question 3: Finish Eating

Implement finish_eating which sets a group's status to 'finished' and frees the table they were using so that the diner has one more free table. If the group's status is already 'finished', this function should do nothing. finish_eating is part of the Group ADT.

def finish_eating(group, diner):
    """
    >>> din = make_diner("Foothill")
    >>> add_table(din)
    >>> g1 = make_group("Nick's Group")
    >>> serve(din, g1)
    >>> num_free_tables(din)
    0
    >>> finish_eating(g1, din)
    >>> num_free_tables(din)
    1
    >>> status(g1)
    'finished'
    >>> finish_eating(g1, din) # g1 has already finished eating so this should do nothing
    >>> num_free_tables(din)
    1
    >>> status(g1)
    'finished'
    """
    "*** YOUR CODE HERE ***"

Use OK to test your code:

python3 ok -q finish_eating

Problem 2: Major League Baseball

Question 4: MLB All Star

It's October, which means its baseball playoffs season! In this exercise, let's utilize dictionaries to see if we can model and learn more about some of our favorite players. In this problem, you will be implementing multiple functions.

As you can see within your hw05.py file, the dictionaries mapping players to their team and statistics respectively have been created already. However, instead of accessing these values directly, we'll be implementing two functions to retrieve the appropriate values as a layer of abstraction.

Implement the get_team and get_stats functions to retrieve the team or statistics given a player's name.

full_roster = {
    "Manny Machado" : "Dodgers",
    "Yasiel Puig" : "Dodgers",
    "Aaron Judge" : "Yankees",
    "Clayton Kershaw" : "Dodgers",
    "Giancarlo Stanton" : "Yankees"
}

full_stats = {
    "Manny Machado": ["SO", "1B", "3B", "SO", "HR"],
    "Yasiel Puig": ["3B", "3B", "1B", "1B", "SO"],
    "Aaron Judge": ["SO", "HR", "HR", "1B", "SO"],
    "Clayton Kershaw": ["1B", "SO", "SO", "1B", "SO"],
    "Giancarlo Stanton": ["HR", "SO", "3B", "SO", "2B"],
}

def get_team(player):
    """Returns team that the provided player is a member of.

    >>> get_team("Manny Machado")
    'Dodgers'
    >>> get_team("Aaron Judge")
    'Yankees'
    """
    "*** YOUR CODE HERE ***"
    

def get_stats(player):
    """Returns the statistics associated with the provided player.
    >>> get_stats("Manny Machado")
    ['SO', '1B', '3B', 'SO', 'HR']
    >>> get_stats('Aaron Judge')
    ['SO', 'HR', 'HR', '1B', 'SO']
    """
    "*** YOUR CODE HERE ***"
    

Use OK to test your code:

python3 ok -q get_team
python3 ok -q get_stats

Submit

Make sure to submit this assignment by running:

python3 ok --submit

Maps

This homework is shorter than normal homeworks in order to give you more time to work on the Maps project. Please get started on it early.