Solutions: You can find the file with solutions for all questions here.

Using Abstractions

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 start_eating(group, diner):
    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:

def add_table(diner):
    """
    >>> din = make_diner("Croads")
    >>> num_free_tables(din)
    0
    >>> add_table(din)
    >>> add_table(din)
    >>> num_free_tables(din)
    2
    """
    diner[1] += 1

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 'table not free'. If there are free tables, the group's status should be updated to 'eating' and the diner should have one less free table.

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)
    'table not free'
    >>> status(g2)
    'waiting'
    """
    if diner[1] > 0:
        diner[1] -= 1
        start_eating(group, diner)
    else:
        return 'table not free'

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

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'
    """
    if group[1] == 'finished':
        return
    group[1] = 'finished'
    add_table(diner)

Use OK to test your code:

python3 ok -q finish_eating

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 hw06.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'
    """
    return full_roster[player]

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']
    """
    return full_stats[player]

Use OK to test your code:

python3 ok -q get_team
python3 ok -q get_stats

Question 5: Merge Dictionaries

Implement the function merge_dict. The merge_dict function merges two dictionaries with the same keys together.

def merge_dict(d1, d2):
    """Returns a dictionary with two dictionaries merged together. You can assume that the same keys appear in both dictionaries.
    >>> data8 = {"midterms":1, "projects":3}
    >>> data100 = {"midterms":2, "projects":3}
    >>> combined_exams = merge_dict(data8, data100)
    >>> combined_exams
    {'midterms': 3, 'projects': 6}
    >>> sunday_orders = {"pizza": 3, "hot dogs": 2, "fries": 5}
    >>> monday_orders = {"pizza": 1, "hot dogs": 1, "fries": 8}
    >>> combined_orders = merge_dict(sunday_orders, monday_orders)
    >>> combined_orders
    {'pizza': 4, 'hot dogs': 3, 'fries': 13}
    """
    result_dict = {}
    for work in d1:
        result_dict[work] = d1[work] + d2[work]
    return result_dict

Use OK to test your code:

python3 ok -q merge_dict

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.