Build a minimal reproduction
Objective
Reduce a failure while preserving the distinction that exposes it. Prerequisites: functions, loops, lists/arrays, and objects/dictionaries in Python.
Reduce a failure while preserving the distinction that exposes it.
Explanation
A long dataset makes it harder to see why an implementation fails. Remove irrelevant elements while keeping the expected and actual results different. Two target occurrences can distinguish first from last; one missing requirement distinguishes all from any.
Write the expected result before changing the code. A minimal reproduction should still exercise the same incorrect assumption as the original failure.
Runnable worked example
The linked exercise begins with flawed code. This repaired example includes a result assertion and an input-preservation assertion.
def solve(values, target):
for i in range(len(values)-1, -1, -1):
if values[i] == target:
return i
return -1
import copy
args = [[2, 1, 2], 2]
before = copy.deepcopy(args)
assert solve(*args) == 2
assert args == before
Expected result: 2.
A single match hides the bug. Two repeated targets create the smallest meaningful difference between first and last. Regression design: Reduce a long failing list to the shortest list that still distinguishes first from last.
Common mistakes
Do not simplify away the duplicate, ordering, or empty-input feature responsible for the failure.
Guided practice
Reduce a long failing list to the shortest list that still distinguishes first from last. Run the exercise starter first and record the actual failure. Change one operation, rerun the old failure and nearby cases, then explain the cause in one sentence. An exception is evidence; a passing happy path alone is not a diagnosis.
Independent repair exercises
Recap
Reduce a failure while preserving the distinction that exposes it. Keep a regression that fails on the old code and passes after the repair. Python and JavaScript tracks teach corresponding debugging concepts with distinct language behavior and independent progress.