# Initialize Otter
import otter
grader = otter.Notebook("quiz8practice-functions-loops.ipynb")
βοΈ Quiz 8 Practice - Using Functions#
This quiz will evaluate your mastery of using functions in Python. Functions provide a way to isolate code that you want to use repeatedly, and they allow you to pass in data to the code and get data back out of the code.
Note The actual quiz will have different questions but will be similar to the ones provided here. All tests are visible to you, so you can test your solution as many times as you want.
Entering Your Information for Credit#
To receive credit for assignments it is important we can identify your work from others. To do this we will ask you to enter your information in the following code block.
Before you begin#
Run the block of code at the top of the notebook that imports and sets up the autograder. This will allow you to check your work.
import pkg_resources
from subprocess import call
package_name = 'ENGR131_Util_2024'
version = '0.1.8'
package_version = f'{package_name}=={version}'
try:
# Check if the package and version are installed
pkg_resources.require(package_version)
print(f'{package_version} is already installed.')
except pkg_resources.DistributionNotFound:
# If not installed, install the package
print(f'{package_version} not found. Installing...')
call(['pip', 'install', package_version])
except pkg_resources.VersionConflict:
# If a different version is installed, you can choose to upgrade/downgrade
installed_packages = {dist.key: dist.version for dist in pkg_resources.working_set}
installed_version = installed_packages.get(package_name.lower())
print(f'{package_name} {installed_version} is installed, but {version} is required.')
# Optionally, upgrade or downgrade the package to the required version
call(['pip', 'install', '--upgrade', package_version])
# DO NOT MODIFY THIS CELL
from ENGR131_Util_2024 import cell_logger, StudentInfoForm, responses, upsert_to_json_file
# Register the log function to be called before any cell is executed
get_ipython().events.register('pre_run_cell', cell_logger)
responses["assignment"] = "quiz_8_practice"
StudentInfoForm(**responses)
Question 1: Thermal Expansion of a Rod
You are a materials engineer tasked with evaluating the suitability of a metal rod for use in a construction project where temperature variations are significant. Itβs essential to estimate the thermal expansion of the rod to ensure that it will not cause structural issues due to temperature changes.
The change in length, \(\Delta L\), of a rod due to thermal expansion can be calculated using the equation:
where:
\(\alpha\) is the coefficient of thermal expansion of the material (in per degree Celsius, \(Β°C^{-1}\)),
\(L_0\) is the original length of the rod (in meters, m),
\(\Delta T\) is the change in temperature (in degrees Celsius, Β°C).
To ensure the structural integrity of the construction, itβs crucial to consider a tolerance factor in the design. This factor adjusts the expected change in length to account for uncertainties in material properties and temperature variations.
If the change in length is \(\Delta L\), the adjusted length change including the tolerance factor is given by:
Write Python code to do the following:
Define a function called
rod_expansion
which accepts three input arguments:alpha
,L0
, andDeltaT
, and a fourth optional argument calledtolerance_factor
which defaults to 1.0.Define a variable called
DeltaL
and set it equal to the result of equation (1) above.Define a variable called
DeltaL_adjusted
and set it equal to the result of the equation (2) above.Have your function print the response:
The change in length of the rod is: <DeltaL> m, and the adjusted length change for tolerance is <DeltaL_adjusted> m
, where<DeltaL>
, and<DeltaL_adjusted>
are the float values of the variable for the change in length and the adjusted change respectively, rounded to 3 decimal places.Implement your function so that it returns the adjusted length change of the rod for a given input.
Provide an example usage of your function by calling it with the following inputs:
alpha = 1.2e-5
Β°C(^{-1})L0 = 2.0
mDeltaT = 50
Β°Ctolerance_factor = 10
Save the output of the function call to a variable adjusted_length_change
.
Your code should print the response:
Note: You can control the precision of the output by using string formatting to round the output to a specific number of decimal places. For a consistent presentation, use three decimal places in your output as shown in the example:
print(f"{DeltaL:0.3f}")
Your code replaces the prompt: ...
# Your function for rod_expansion goes here
...
# Example usage:
alpha = ...
L0 = ...
DeltaT = ...
tolerance_factor = ...
# Call the function with example inputs and save to a variable `adjusted_length_change`
...
grader.check("q1-thermal-expansion-rod")
Question 2: Identifying the First Acidic Solution with pH Below a Certain Level 5
Write a Python function acidic_pH_detection
that takes a sequence of pH level measurements (represented as a list of floats) as a variable pH_levels
. The goal is to identify the first measurement that falls below a predefined threshold
value, indicating the occurrence of an acidic solution. Once an acidic solution with a pH below the threshold is found, the program should return the index (position) of this measurement in the dataset and the value of the pH level, as a tuple then terminate the search.
Requirements:
Write a function
acidic_pH_detection
that takes two input arguments:pH_levels
(a list of floats) andthreshold
(a float).Use a for loop to iterate through the
pH_levels
list.Within the loop, implement a condition to check if the current pH level is below the threshold.
Print the index of the first pH level that is below the threshold and the value of this pH level. The print statement should read
First acidic solution detected below threshold found at index <index> with pH <value>.
, whereand are the index and pH level of the first measurement below the threshold. If a pH level below the threshold is found, use a return statement to exit the loop immediately and return a tuple containing the index of the measurement and the value of the pH level.
If no pH level below the threshold is detected, print a message:
No acidic solution was detected below the threshold of <threshold>.
, whereis the value of the threshold.
Test your function by calling it with the provided inputs. Save the output of the function call to a variable
acidic_solution
.
Hints:
Use the
enumerate()
function to get both the index and the value of each item when iterating through the list with a for loop.
# Sample pH level measurements
pH_levels = [7.2, 5.5, 6.8, 7.0, 7.3, 4.8, 3.2, 7.4, 8.0, 6.5, 4.9, 3.5]
# Threshold value for acidity
threshold_pH = 5.0
...
grader.check("q2-detecting-threshold-signal")