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.


Python
def peak_abs_mm(samples):
    largest = 0.0
    for i in range(len(samples) - 1):
        largest = max(largest, abs(samples[i]))
    return largest


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.

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Software Debugging Questions: