Shrink the input
Why it matters A crash on a 200-row file is a puzzle. The same crash on a 2-row file usually names its own cause. Shrinking is boring, mechanical work, so let code do it.
You have a list that makes some code fail, and a function fails(xs) that says whether a list still triggers the failure. You want the smallest list you can get by removing items.
1-minimal: a failing input where removing any single item makes it stop failing. It is not always the smallest possible input, but it is small enough to read, and it can be found quickly.
Try removing each item in turn and keep the removal if the list still fails. That alone is a good shrinker. Removing big chunks first (halves, then quarters) is faster on long inputs: this is the idea behind delta debugging, and property-testing tools such as Hypothesis shrink failing inputs the same way.
fails = lambda xs: 13 in xs and 42 in xs
shrink([5, 13, 8, 42, 9], fails) # -> [13, 42]Write shrink(xs, fails). Return a list made of items from xs in their original order, for which fails is true, and from which removing any single item makes fails false (1-minimal). You may assume removing items never makes a passing list fail again. Budget: each call to fails runs a slow test, so for 200 items you may call it at most 3,000 times.
Spotted a problem in this lesson? Report it · Ask about this lesson
This exercise opens soon
Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.
This browser is blocking cookies for learn.floati.life, so lessons you finished here cannot open in order. Allow cookies for this site and reload.