Homework 2 Python Programming#
Instructions:
LLM Not Permitted (Check syllabus for details)
Here is how you can find python help
Answer all questions in one notebook
Rename notebook file name to
HW1_YourFirstName_YourLastName
Submit your notebook and other required files to Canvas by the due date on Canvas
100 points (20 points each)
Problem 1 - For-loop and indexing#
Calcuate daily streamflow concentrations of a contaminant over a week, 7-day period. The contaminant’s concentration is measured in milligrams per liter (mg/L). You are given the following information:
Initial concentration: 0.5 mg/L
Daily increase: 0.02 mg/L
Degradation rate: 10% per day
This means that after the daily increase of 0.02 mg/L, 10% of the contaminant is removed from the stream each day. Note that your initial concentration is your initial condition at time zero.
Tasks:
Create a list called
concentrations
that you can use to store the daily concentrationsUse a for-loop to calculate the 7-day period:
Calculate the concentration for each day
Append the calculated concentration to the
concentrations
list
Use f-strings and print the following information
calculated concentration for each day with two decimal places
minimum concentration over the one week period with two decimal places
Output:
Your output should look something like this
Day 0: 0.50 mg/L
Day 1: 0.47 mg/L
Day 2: 0.44 mg/L
...
Day 7: 0.33 mg/L
The minimum concentration is 0.33 mg/L
Note the formatting of the outputs.
# Initial values
initial_concentration = 0.5 # mg/L
daily_increase = 0.02 # mg/L
degradation_rate = 0.1 # 10%
#Creating a list of concentrations
#Calculate the concentrations for the analysis periods
#Minimum concentration
Problem 2 - If condition#
You are analyzing a big dataset about water quality in a river based on nutrient levels (total nitrogen and total phosphorus), oxygen availability for aquatic life (dissolved oxygen), and bacterial contamination (fecal coliform bacteria). High nutrient levels and low dissolved oxygen levels indicate ecosystem imbalance and stress. High nutrient levels of nitrogen and phosphorus lead to algal blooms and eutrophication, depleting oxygen in the water. Dissolved oxygen is also necessary for fish, macroinvertebrates, and other aquatic life to breathe. Presence of fecal coliform bacteria indicates contamination from human or animal waste. This poses health risks to humans and animals when using the water for recreation or drinking.
This is an index for nutrients based on total nitrogen, total phosphorus, and dissolved oxygen.
nutrient_index = (total_nitrogen x 0.3) + (total_phosphorus x 0.7) + (10/dissolved_oxygen)
Where:
total_nitrogen
,total_phosphorus
, anddissolved_oxygen
are measured in mg/LWeights of 0.3 and 0.7 are applied to nitrogen and phosphorus based on their relative impacts
dissolved_oxygen
is inverted on a scale of 10 to account for low DO indicating poor quality
This is an overall water quality classification with just two parameters, nutirmt index and fecal coliform:
Good Water Quality:
- Nutrient Index: <2.5
- Fecal Coliform: <200 MPN/100mL
Moderate Water Quality:
- Nutrient Index: 2.5 -3.5
- Fecal Coliform: 200-1000 MPN/100mL
Poor Water Quality:
- Nutrient Index: >3.5
- Fecal Coliform: >1000 MPN/100mL
Using if-condition statements, write a python code that takes the above four water quality parameters and outputs:
classificaion of nutrient index (good, moderate, or bad)
classification of the overall water quality (good, moderate, or bad)
Here is a sample input and output that your code can produce:
Input: total_nitrogen = 1.2
Input: total_phosphorus = 0.01
Input: dissolved_oxygen = 4.5
Input: fecal_coliform = 1200
Output: Given a total nitrogen of 1.2 mg/L, total phosphorus of 0.01 mg/L, and dissolved oxygen of 4.5 mg/L, the nutrient index is 2.35 indicating moderate water quality with respect to nutrient index.
Output: Given a nutrient index of 2.35 and fecal coliform concentration of 1200, the overall water quality in the river is poor.
Your code should produce the same outputs with the same format, but your numbers and water quality classifications will be different for each new sample. Solvle the problem for the water quality data sample given below.
# water quality data sample
total_nitrogen = 0.8
total_phosphorus = 0.044
dissolved_oxygen = 5.3
fecal_coliform = 800
# Calculate nutrient index
# Classif water quality based on nutrient index
# Print results of nutrient index
# Classify overall water quality
# Print results of overall water quality
Problem 3 - For loop and if condition#
You have collected a series of water quality measurements from a local stream. Each measurement includes dissolved oxygen (DO), pH, and turbidity values. Using for-loop and if condition, write a python code that counts the number of samples the has any parameter that exceeds the critical measurment.
Critical measurement for each parameter:
DO< 4 mg/L
pH< 6.5
pH> 8.5
Turbidity > 20 NTU.
Print messages indicating the number of critical measurements found. You answer should look something like this:
Number of samples with critical measurements: 3
# DO, pH, and Turbidity measurements for four samples.
measurements = [
[6.2, 8.6, 15], # DO, pH, and Turbidity measurements Sample # 1
[3.8, 6.9, 22], # DO, pH, and Turbidity measurements Sample # 2
[5.1, 6.1, 18], # DO, pH, and Turbidity measurements Sample # 3
[6.2, 7.1, 17], # DO, pH, and Turbidity measurements Sample # 4
]
# Counter for critical measurements
# Loop through each measurement
Problem 4 - Functions#
You want to analyze rainfall data from a series of sensors located at different points in a river basin. The data is represented as a list of lists, where each inner list contains measurements from a sensor for a day. Each sensor records rainfall in millimeters (mm).
rainfall_data = [
[0, 2.5, 12.1, 0, 1.0, 0, 0], # Sensor 1 data for 7 days
[1.2, 0, 0, 3.4, 0, 0, 4.5], # Sensor 2 data for 7 days
# ... more sensors
]
Create the function rainfall_analysis
that takes a sensor measurements as input and outputs for each sensor the:
average rainfall over the observed period
day with the highest rainfall
number of days with no rainfall
In other words, your function will have one input (a list) and three outputs (floats).
Use print funnction with f-formatting to make your output look like this:
Station # 1 recorded average rainfall of 2.23 mm with 4 dry days and max rainfall was recorded on Wednesday
Station # 2 recorded average rainfall of 1.30 mm with 4 dry days and max rainfall was recorded on Sunday
Station # 3 recorded average rainfall of 2.87 mm with 2 dry days and max rainfall was recorded on Saturday
Station # 4 recorded average rainfall of 2.16 mm with 1 dry days and max rainfall was recorded on Sunday
Station # 5 recorded average rainfall of 0.64 mm with 6 dry days and max rainfall was recorded on Monday
Hint: This problem will help reinforce your understanding of defining and calling a function, for-loops, if-conditions, and some of the list methods such as list.index()
, and python functions that you learned. These are what you need to solve this problem.
# Dataset for five sensors starting from Monday for each sensor
rainfall_data = [
[0, 2.5, 12.1, 0, 0, 0, 0], # Sensor 1 data for 7 days
[0.1, 2.3, 13.1, 3.4, 0, 0, 4.5], # ...
[0.2, 0, 0, 3.4, 5.0, 6.0, 5.5],
[1.2, 1, 4, 3.4, 1, 0, 4.5],
[4.5, 0, 0, 0, 0, 0, 0], # Sensor 5 data for 7 days
]
# rainfall analysis function
#rainfall analysis
Problem 5 - Script files#
Resolve Problem 4 but using a script file.
Create a new script file and rename is to “YourFirstName_YourLastName_HW2_P5.py”
Write three functions to calculate:
average rainfal
averageRainfall
highest rainfall per day `
Number of days with no rainfall
Add your three functions to your script file
Add a docstring to one of your three functions
Import your three functions and resolve Problem 4
Use
help()
to show the docstring of your function
When you submit your homework on Canvas do not forget to submit your script file along with this file. Also, make sure that your function names do not match any of your variable names.
#rainfall analysis