A numerical comparison tool uses the following Python functions. All inputs and outputs in this example are dimensionless.
def shifted_square(x): return (x - 1.0) ** 2 def doubled(x): return 2.0 * x def output_change(model, x0, x1): return model(x1) - model(x0)
Each table row represents a call to output_change with the listed function and numerical arguments. Complete the returned changes as exact integers. A function name without parentheses passes the function itself as an argument.
Unlock full course notes by signing up
Recognize a keyword-only interface
An engineer writes this Python function to calculate pressure from force in newtons and area in square metres:
Complete the statements about how its interface controls the following calls. Use the standard Python terminology and exact exception name.
In pressure(240.0, area_m2=0.006), the value for area_m2 is supplied as a ________. In contrast, pressure(240.0, 0.006) raises ________ before the function body executes.
Separate local adjustment from caller state
A force sensor correction subtracts a supplied zero offset plus an additional 2.0 N calibration adjustment. Consider this Python 3 code:
After both calls finish, what are the values of first_n, second_n, and offset_n, respectively? All three values are in newtons.
Audit the complete engineering function contract
An engineering function has this Python 3 signature:
Its contract requires nonnegative flow in litres per second, nonnegative pressure increase in kilopascals, and an efficiency satisfying 0<η≤10<\eta\leq10<η≤1. All inputs are finite real numbers. Any input outside this domain must raise ValueError; otherwise, the function must return input power in kilowatts.
Use
Track a modified input and separate returned data
An engineer stores temperature readings in degrees Celsius and uses this Python function to add a reading and calculate deviations from the updated mean:
After every statement has executed, report average, samples[0], len(samples), and deviations[2]. Give the average, first sample, and last deviation in degrees Celsius to one decimal place. Give the number of samples as an exact integer. Python list indices start at zero.