Mark Yourself Question
A function is specified to calculate energy in joules from constant power in kilowatts and duration in minutes. Its intermediate variables must represent power in watts and duration in seconds because those values are also used in diagnostic reports.
def energy_j(power_kw, duration_min): power_w = power_kw * 60.0 duration_s = duration_min * 1000.0 return power_w * duration_s
For power_kw = 1.8 and duration_min = 2.5, an existing test expects 270000 J270000\ \text{J}270000 J and passes. Assume finite positive inputs, exact arithmetic for the algebraic comparison, and no overflow.
a) Trace both intermediate values and the returned energy. Explain why the energy test passes despite incorrect conversions.
b) Correct both conversions and calculate the repaired intermediate values and returned energy.
c) Determine whether testing only the returned energy with additional inputs can distinguish these two implementations under the stated arithmetic assumptions. Propose checks that would detect the intermediate defects.
Unlock full course notes by signing up
Repairing a squared-unit conversion
A function should return tensile stress in megapascals from force in newtons and area in square millimetres:
Use σ=F/A\sigma=F/Aσ=F/A. For a force of 7500 N7500\ \text{N}7500 N and an area of 400 mm2400\ \text{mm}^2400 mm2, determine the result the function should return after correcting its unit conversion. Give your answer in MPa, to two decimal places.
Tracing a calculation that remembers earlier calls
A sensor-checking function should return the mean signed pressure error for the readings supplied in its current call. Each error is a reading minus the reference pressure, and all pressures are in kPa.
The following Python code executes once. The three calls then run in the order shown, without restarting the program or clearing the list.
Complete the table with the number of errors stored immediately after each call, the mean actually returned, and the mean required by the specification. Enter exact integers; the two mean columns are in kPa.
Choosing tests that expose different defects
A Python function must return the largest absolute displacement from any nonempty list of readings in millimetres. The readings are finite real numbers.
A proposed repair changes the loop to range(len(samples)) and retains abs. Another proposed shortcut replaces the entire function body with return max(samples).
Which pair of tests would make the original function fail at least one test and the shortcut fail at least one test, while the proposed loop repair passes both? All expected results below are in millimetres.
Finding why a shrinking search stops progressing
An integer-search routine should locate target within the inclusive interval from low to high. The initial target is guaranteed to lie in that interval. A student reports that the following Python loop never finishes:
For the nonnegative integers used here, // discards any fractional part. Count an iteration whenever the loop body executes.
Find the first iteration on which neither bound changes, and give low and high immediately after that iteration. Then the assignment to low is repaired as low = mid + k. Determine the largest positive integer k for which low = mid + k guarantees progress and retains every possible integer target for any valid initial interval under the stated assumptions. Keep the other statements unchanged.