EEB125 Midterm - Wednesday February 12 2025#
Read this section before starting the test
Test Instructions#
This test has 3 parts each with multiple questions.
Complete all questions (of all parts) below.
The answers to the questions will be submitted on MarkUs using a similar workflow to the homework assignments, except that MarkUs will not give you feedback on passing or failing the autotests.
Answers where you are asked to write Python code will be autograded, and written answers will be graded manually by the teaching team.
Marking Rubric#
Part 1 (10 points total) |
Q1 (3 points) |
Q2 (3 points) |
Q3 (4 points) |
|
|---|---|---|---|---|
Part 2 (15 points total) |
Q1 (2 points) |
Q2 (3 points) |
Q3 (2 points) |
Q4 (8 points) |
Part 3 (10 points total) |
Q1 (4 points) |
Q2 (6 points) |
The total number of marks for this test is 35 points.
Aids Allowed and Academic Integrity#
You are allowed to use any materials from the course or any other written sources (e.g., books, websites).
You are not allowed to directly receive or give help during the test period. In other words, all work must be your own, and you must not discuss or post any information about this test with anyone during the test period. You may not use AI assistants for the test.
As a student, you are responsible for ensuring the integrity of your work and for understanding what constitutes an academic offense.
Time Allowed#
The test will be available at 1:10 PM and must be submitted on MarkUs (see Submission Instructions) by 3:00 PM. MarkUs has been set up to allow tests to be submitted up to 3:05 PM without penalty to allow for minor technical difficulties.
Late tests will receive a grade of zero unless you have an approved accommodation from your instructor.
How do I ask a question during the test?#
The teaching team will be available in the lecture room during the test (WI 1017) in case you have any questions during the midterm.
Please check Quercus announcements for a zoom link to ask questions if you are taking the test remotely.
You are responsible for checking Quercus announcments during the test, since that is where we will post any announcements that affect everyone writing the test.
The class discussion forum on Piazza will be disabled during the test period.
Submission Instructions#
You will submit your work on MarkUs. To submit your work:
Download this file (
Midterm.ipynb) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)Submit this file to MarkUs under the midterm assignment. (See our MarkUs Guide for detailed instructions.) All homeworks will take place in a Jupyter notebook (like this one). When you are done, you will download this notebook and submit it to MarkUs.
Note: unlike the homeworks, there is no autograding preview for this midterm.
Part 1: Coding Exercises (10 points)#
Part 1, Question 1 (3 points)#
Create the following three variables:
name: a string containing your full nameno_space_name: a string in which thereplace()method is used to replace each space innamewith an underscore ‘_’.name_len: an integer that is the length ofname
You may wish to verify your work by running print() on each of your newly created variables, and using type() to verify the type of each.
# Write your code in this cell
# This code is provided to help check your work
print(f"Your name is {name}")
print(f"Your name with dashes instead of spaces is {no_space_name}")
print(f"The length of your name is {name_len}")
Part 1, Question 2 (3 points)#
Given a list of floating point numbers in values:
Compute the number of values in the list that are less than 0, and store that in a variable named
below_zero.Store the smallest value in a variable named
smallest_value.
Your solution should still work if we changed the list stored in values.
# These variables are provided for you (to avoid copy-and-paste errors)
values = [22.5, -42.3, 23.5, -1.1, -2.8]
# Write your code below
# This code is provided to help you check your work
print(f"Count: {below_zero}")
print(f"Smallest: {smallest_value}")
Part 1, Question 3 (4 points)#
We have given you a long string in the cell below. You have 3 tasks:
First, split the string into sentences using the
split()method, and store the resulting list in a variable calledsentences.Then create an empty list called
lengths. Using a loop, iterate oversentences, appending the number of words in each sentence tolengths. (Hint: you will need to usesplit()again)Store the sentence with the largest number of words in a variable named
longest.
Do not hard-code your answers, we are looking for your use of Python methods, functions, and loops.
# This variable is given to you: do not change it!
winter = "Snow blankets the ground. Ice glazes the windows. Fire crackles warmly in the hearth. Boots crunch in the snow. Scarves wrap tightly. Stars shine brightly."
# Write your code here
# This code is provided to help you check your work
print(sentences)
print(lengths)
print(longest)
Part 2: Mammalian Reproductive Strategies (15 points)#
Part two of this test will examine the average length of the gestation period for mammalian species. This is expressed in units of days. For example, a human fetus may spend approximately 270 days, on average, in utero.
To begin, run the following code cell.
# The code in this cell is provided to get you started on this part.
# RUN THIS CELL, but do NOT modify the code.
gest_file = open("gestation.csv", "r")
gest_lines = gest_file.readlines()
gest_header = gest_lines[0]
gest_data = gest_lines[1:]
Part 2, Question 1 (2 points)#
a) What does the open() function do? Please explain what information each argument given ("gestation.csv" and "r") is communicating to Python. (1pt)
WRITE YOUR ANSWER HERE
b) Please explain what the Python function readlines() does. (1pt)
WRITE YOUR ANSWER HERE
Part 2, Question 2 (3 points)#
To start, run the following code cell.
# The following code is provided for you.
# Run the cell, but do NOT change any of the code.
gest_times = {}
for line in gest_data:
line_data = line.strip().split(",")
species = line_data[1]
gest_time = line_data[2]
if gest_time != "NA":
gest_times[species] = float(gest_time)
Q2. Describe the line of code: if gest_time != 'NA'. What does 'NA' mean in this context? What is this if statement accomplishing? (3pts)
WRITE YOUR ANSWER HERE
Part 2, Question 3 (2 points)#
In the code cell below, complete the function mean to calculate the statistical average of its argument pop. You may assume that pop is not empty. The provided code below assumes that you will store the computed average in a variable named average before returning it.
def mean(pop):
# Return the average of the values in the list pop.
# Complete this function.
return average
Part 2, Question 4 (8 points)#
a) Please use your newly-defined function to calculate how long mammalian species take to gestate, on average. You will want to use as input gest_times.values(). Store the result in a variable called mean_gestation_time. (1pt)
# Write your code here
# This code is provided to help you check your work
print(f"The average gestation time is {mean_gestation_time} days.")
Run the code below to create a dictionary that links the name of each species in the dataset to its order.
sp_ord = {}
for line in gest_data:
line_data = line.strip().split(",")
order = line_data[0]
species = line_data[1]
sp_ord[species] = order
b) In the code cell below, please create a list with all of the unique orders represented in the dataset. Assign it to the variable unique_orders. (2pts)
# Write your code here
# This code is provided to help you check your work
print(unique_orders)
c) Please combine the information from the dictionaries gest_times and sp_ord to find the gestation times of all species from the order “Primates”. Calculate the average gestation time for all Primate species and assign the result to the variable primate_mean_gestation. (5pts)
(This question does not depend on unique_orders)
# Write your code here
# This code is provided to help you check your work
print(primate_mean_gestation)
Part 3. Gestation time and Conservation Risk (10 points)#
Next, we are going to examine what impact gestation time seems to have on a species’ conservation risk. Our underlying question is: Do at-risk species tend to take a shorter or longer amount of time in utero?
To start, run the following code cell.
# The following code is provided for you.
# Run the cell, but do NOT change any of the code.
iucn = open("iucn_status.csv", "r")
iucn_lines = iucn.readlines()
iucn_header = iucn_lines[0]
iucn_data = iucn_lines[1:]
iucn_map = {'LC': 1, 'NT': 2, 'VU': 3, 'EN': 4, 'CR': 5, 'EW': 6, 'EX': 7, 'DD': 0}
sp_threat = {}
for line in iucn_data:
line_data = line.strip().split(",")
species = line_data[1]
iucn_risk = line_data[2]
risk_numeric = iucn_map[iucn_risk]
threat = False
if risk_numeric >= 3:
threat = True
elif risk_numeric == 0: # Skip over any lines that are level DD ('data deficient')
continue
sp_threat[species] = threat
print(sp_threat)
Part 3, Question 1 (4 points total)#
a) Please describe the contents of the dictionary sp_threat after running the block of code above. What python data types are the keys and values? What do these mean, biologically? (2pts)
WRITE YOUR ANSWER HERE
b) Please explain why we included the conditional statement if risk_numeric >= 3: (2pts)
WRITE YOUR ANSWER HERE
Part 3, Question 2 (6 points)#
Start by running the code block below.
# The following code is provided for you.
# Run the cell, but do NOT change any of the code.
import math
def variance(data, mean_val):
diffs = []
for i in data:
diff = i - mean_val
sq_diff = diff ** 2
diffs.append(sq_diff)
return mean(diffs)
def st_dev(data, mean_val):
var = variance(data, mean_val)
sd = math.sqrt(var)
return sd
risk_gest = {True: [], False: []}
for sp in sp_threat:
try:
gest_time = gest_times[sp]
threat = sp_threat[sp]
risk_gest[threat].append(gest_time)
except:
continue
for threat in risk_gest:
sizes = risk_gest[threat]
if len(sizes) == 0:
continue
mean_size = mean(sizes)
sd_size = st_dev(sizes, mean_size)
print(f"{threat}: mean size {mean_size}, standard deviation {sd_size}")
a) The code block above uses exception handling (try: and except:). Please explain why we needed to use exception handling to link the two dictionaries gest_times and sp_threat by their keys. (3pts)
WRITE YOUR ANSWER HERE
b) The code above calculates the mean gestation time among species within each threatened category, as well as its standard deviation. True represents species that ARE at risk. False represents species that are NOT at risk.
Do species that are at risk tend to have shorter gestation times than those that are not at risk? Create an argument based on both the mean and standard deviation. (3pts)
WRITE YOUR ANSWER HERE