Lab 5
Objectives
By the end of the lab, you will have:
- Gained practice working with lists
- Gained practice working with dictionaries
- Continued practicing
for
loops - Learned how to perform basic data analysis in Python
In this lab, we are going to write code to find the month with the highest average temperatures in Middlebury in 2022. We’ll take you through the steps you’ll need to take to do that, starting with the dictionary in data.py
.
This lab does not allow for any imports other than data
, and does not allow for the use of any built-in functions besides len
, type-conversion functions (e.g., int
), and functions for accessing keys/values from dictionaries (e.g, .items
or .keys
)
Part 0: Setup
To begin, download the file lab05.zip
by clicking the blue “Download Starter Files” button at the top of the lab, and extract the files from the zip file into a directory called lab05
inside your labs
directory.
Part 1: Computing Means
For the first part of this lab, you’ll write a function mean
that computes the mean of numeric values in a list. The mean is computed by taking the sum of a collection of numbers, then dividing the sum by the number of numbers in that collection.
mean
|
|||
---|---|---|---|
Parameters |
|
||
Returns |
float
|
- Step one: Define the function and make sure it takes a list argument
numbers
. Don’t forget your docstring! - Step two: We’ll break down the definition of a mean to complete this function. Let’s start with “taking the sum of a collection of numbers”. We recommend implementing this using a
for
loop over the numbers in the list. It turns out you’ve already seen a function to do this in our lecture:list_sum
! Write out that code, and ask if you and your partner are unsure about how the code works. - Step three: Now let’s think about the second part of the definition of the mean, “dividing the sum by the number of numbers in that collection”. Think about what variable represents “the sum” in your code, and divide that by the number of items in your list. You should be able to get the denominator easily using a function that you are very familiar with.
- Step four: Test your function on the examples and confirm that it works before moving on.
Examples
>>> mean([1])
1.0
>>> mean([-5, -3, 8, 3])
0.75
Part 2: Dictionary Manipulation
Next, we’re going to write a function that takes in a dictionary as input, where the keys are strings and the values are lists of floats. A simple example of such a dictionary is provided below, while a more complex example is provided in data.py
.
{"shorter": [1.0],
"longer": [-5.0, -3.0, 8.0, 3.0]
}
Our goal will be to return a dictionary with the same keys, where the values are the means of the original lists. So for the simple example,
{"shorter": 1.0,
"longer": 0.75
}
mean_of_values
|
|||
---|---|---|---|
Parameters |
|
||
Returns |
Dict[str, List[int/float]]
|
As you may have guessed, we’re going to use the mean
function we already wrote to help us out. Follow the steps below:
- Step one: Define the function and make sure it takes a dictionary argument
dictionary
. Don’t forget your docstring! - Step two: Create a new empty dictionary
new_dict
below your docstring. We expect you to not modify the original dictionary in this function. - Step three: Let’s start by just copying everything from
dictionary
tonew_dict
, then we’ll think about how to compute the mean. Write a loop that goes through the keys and values of the original dictionary usingfor key, value in dictionary.items()
. In the body of your loop, set the value ofkey
tovalue
innew_dict
, then returnnew_dict
. - Step four: Test out your function in its current state and confirm that it returns a copy of the original dictionary.
- Step five: Now, let’s compute the means! Rather than just setting the
key
tovalue
in the body of your loop, use themean
function to make the value the mean of the original value. - Step six: Test your function on the examples and confirm that it works before moving on.
Examples
>>> mean_of_values({"shorter": [1.0], "longer": [-5.0, -3.0, 8.0, 3.0]})
"shorter": 1.0, "longer": 0.75}
{>>> mean_of_values(data.monthly_temps)
"April": 44.42, "August": 68.80322580645164, "December": 30.86451612903225, "February": 23.357142857142854, "January": 15.88709677419355, "July": 69.7290322580645, "June": 62.470000000000006, "March": 33.29354838709677, "May": 59.04193548387097, "November": 42.51333333333333, "October": 49.532258064516114, "September": 58.52333333333333} {
Part 3: Maximization
Our next function will return the key in a dictionary with the highest value. We’ll use this with our weather dictionary to determine the month that is the hottest, on average.
key_with_max_value
|
|||
---|---|---|---|
Parameters |
|
||
Returns |
str
|
Follow the steps below:
- Step one: Define the function and make sure it takes a dictionary argument
dictionary
. Don’t forget your docstring! - Step two: We are going to start by solving a (slightly) simpler problem than our end goal: we’ll write a function that returns the highest value in the dictionary. Our goal is to go through each value and update our maximum if it’s greater than the highest value we’ve seen so far. Therefore, we’ll start by creating a variable called
max_so_far
, and set it to the lowest possible value in Python. The lowest possible number is negative infinity, and it turns out that there’s a way to represent it in Python:-float("inf")
. - Step three: Using the same loop structure that you saw in part 2, loop through the keys and values of the dictionary. If the current value is greater than the
max_so_far
, update themax_so_far
to be the current value. - Step four: Once your loop is done executing, the
max_so_far
will be the max value overall (since you’ve seen everything)! So, return it. Pause for a minute to check your work - does your function return the highest value from a dictionary that is given as input? - Step five: Now we’ll expand our function to return the key that maps to the highest value in the dictionary. Create a variable
max_key
at the start of your loop (it doesn’t really matter what you set it to, butNone
is a good choice). You’ll note that every timemax_so_far
is updated, the maximum key so far should be updated to the key mapping to the current value. So, add a line to your conditional that setsmax_key
to the current key. - Step six: Update your
return
statement to return the key, rather than the value - Step seven: Test your function on the examples and confirm that it works before moving on.
Examples
>>> key_with_max_value({"shorter": 1.0, "longer": 0.75})
"shorter"
>>> key_with_max_value(mean_of_values(data.monthly_temps))
"July"
Part 4: Putting it Together
Finally, write a function main
that calls the mean_of_values
function and the key_with_max_value
function to allow you to print the month with the warmest average temperature in Middlebury in 2022, using data.monthly_temps
. When the function is called, it should print (not return!) the following message:
The warmest month was ____
You should fill in the ____ with the warmest month according to your calculations.
main
|
|
---|---|
Parameters |
|
Returns |
None
|
Turning in your work
One you are done, submit lab05.py
to gradescope and ensure that you have passed all of the tests. Your file should contain four functions: mean
, mean_of_values
, key_with_max_value
, and main
.