Code

A Thousand Trials

Example-based tests check the cases you thought of. But bugs live in the cases you didn't think of. Make a habit of trying edge cases: empty input, one item, duplicates, negatives, zero, huge values.

Property-based testing

Instead of picking exact answers, describe properties that must always hold, then throw lots of random inputs at the code. For a sort function:

  1. The output is in order: each item <= the next.
  2. The output has exactly the same items as the input (same counts).
from collections import Counter
Counter([3, 1, 3]) == Counter([1, 3, 3])   # True: same items
Counter([3, 1, 3]) == Counter([1, 3])      # False: lost a 3

Always use a seeded random generator (random.Random(42)) so a failure can be reproduced exactly.

Pip tip: On a real project, the Hypothesis library does this brilliantly. It even shrinks a failing input down to the smallest example that still breaks.

Assignment

Finish check_sort(sort_fn, trials=200). It already generates random lists (lengths 0–8, values −5…5). For each one, return False if the result isn't in non-decreasing order or doesn't contain exactly the same items. If every trial passes, return True.