PS05

Contents

PS05#

DS256, Gettysburg College

Prof Eatai Roth

Due: Oct 24, 2025 5p (soft deadline…you may hand in up until the following Monday.)

The questions are in no particular order in terms of difficulty, so if you feel stuck, skip the question and come back to it.

Your Name: …

Collaborators: …

By submitting this work, you attest that:

  • it reflects your own effort and understanding of the material

  • you did not ask for help from students outside the course, other than PLAs

  • you did not use a generative AI to produce any code used below.

# Initialize Otter
import otter
grader = otter.Notebook("ps05.ipynb")

1. Write the function combo_menu that takes as input two dictionaries: entree and side. The keys to the dictionary are the names of menu items. The values are the prices of the menu items.

The function should return a new dictionary called combo that has keys in the form of “ and ” and the value is the cost of the combo (the sum of the entree and side costs).

def combo_menu(entree, side):
...
    

# Test your code, does it do what it should?
entree = {'Burger': 5, 'Taco': 3}
side = {'Fries': 2, 'Salad': 1}

expected = {
    'Burger and Fries': 7,
    'Burger and Salad': 6,
    'Taco and Fries': 5,
    'Taco and Salad': 4,
}
assert combo_menu(entree, side) == expected
grader.check("q1")

2. Write the function update_inventory() that takes as input the dictionary inventory, requested item as a string, and quantity as an integer and returns the updated inventory.

  • inventory a dictionary with keys representing items in stock and values representing quantities in stock

  • item is a string representing the name of a requested item

  • quantity is an integer representing the number of items being added to or removed from inventory. If you are adding inventory for an item that wasn’t in inventory before, add that item to the dictionary with the correct quantity. If you are removing items from inventory and there aren’t enough, remove the item entirely from the inventory.

Tip: To remove a key from a dictionary, you can use the function my_dict.pop(key_to_remove).

def update_inventory(inventory, item, quantity):
...

# Tests for update_inventory
shop = {}
shop = update_inventory(shop, 'Snickers', 10)
shop = update_inventory(shop, 'Milky Way', 4)
shop = update_inventory(shop, 'Milky Way', 2)
shop = update_inventory(shop, 'Snickers', -12)
display(shop)
# The shop should just have 6 for shop['Milky Way']
grader.check("q2")

3. Write the function assignment_mean that takes as input a class roster as dictionary and an assignment name as a string and returns the average grade for that assignment.

  • The keys to the class roster dictionary are student names

  • Each student contains a dictionary of assignments (a dictionary of dictionaries). The values of the .

  • If an assignment is missing, it’s marked with an ‘M’. Otherwise there is a number for the grade.

  • If all assignment is missing for every student, return (NOT print) the letter ‘M’

Below is an example roster.

Tip: first extract the specific grade from each student as a list (excluding missing assignments), then convert the list to a numpy array to take the average.

roster = {
    'Argyle': {
        'ps01': 9,
        'ps02' : 10,
        'ps03' : 8.5,
    },
    'Belinda' : {
        'ps01' : 10,
        'ps02' : 9,
        'ps03' : 'M'
    },
    'Camilla' : {
        'ps01' : 10,
        'ps02' : 10,
        'ps03' : 10,
    },
    'Dana' : {
        'ps01' : 'M',
        'ps02' : 5,
        'ps03' : 'M'
    }
}

# Getting an assignment grade for a student
roster['Belinda']['ps03']
import numpy as np

def assignment_mean(roster, assignment):

...



assignment_mean(roster, 'ps01')
grader.check("q3")

4.

I provide data for the max temperature in Philadelphia from 01/01/2000 through 12/12/2024.

First I’ll do some processing on the data.

# number of days between 01/01/2000 and 12/31/2024
numdays = 365*25 + 7

data_url = 'https://raw.githubusercontent.com/GettysburgDataScience/datasets/refs/heads/main/philly_weather.csv'
maxtemps = np.loadtxt(data_url, skiprows = 21735, max_rows = numdays, delimiter = ',', usecols = [2])
dates = np.loadtxt(data_url, skiprows = 21735, max_rows = numdays, dtype = str, delimiter = ',', usecols = [0])

display(dates[0:5])
display(dates[-5:])
# Let's look at February 2000
dates[31:61]

Leap years have one more day than other years. For this problem, we’ll delete the leap day so that all years have the same length.

# find leap days, then invert to find all days except leap days
leap_days = np.char.startswith(dates, '02/29')
not_leap_days = ~leap_days # ~ is not but applies to every element of an array

# Keep only the days that are not leap days
dates = dates[not_leap_days]
maxtemps = maxtemps[not_leap_days]

# Let's look at February 2000 again to check
dates[31:61]

dates = dates.reshape(-1, 365)
maxtemps = maxtemps.reshape(-1, 365)

dates.shape

Instructions for you

The data are now in a 24x365 numpy array where each row represents the daily max temperature for the year. The indices for the year match the year itself (e.g. 0 –> 2000, 24 –>2024)

Using the maxtemp array, create the following two figures:

  • A figure with two overlayed histograms for the years 2000 and 2024.

  • A bar graph with the year on the x-axis and the number of days over 90 degrees as the bar height.

Submission#

When you are ready to submit, uncomment (cmd+/ or ctrl+/) and run the cell below.

# !git add ps05.ipynb
# !git commit -m 'ps04 submitted'
# !git push -f