Python 3D Academyবাংলা

Testing your own solution: edge cases and small examples

🌍 In real life. Before a new footbridge opens, engineers don't only walk across it on a calm day. They load it with the heaviest crowd allowed, test it empty, and push it in strong wind: the unusual cases are where things break. Code is the same: the example in the problem usually works, and the bug hides in an empty list, a single item or all-negative numbers.

The idea in one sentence: list the edge cases the limits allow and check each by hand, then compare your solution with a simple brute force on many random small inputs, and when they differ, look at the smallest input that shows it.

Its words:

  • edge case: an input at the border of what is allowed: empty, one item, all equal, all negative, the biggest size
  • oracle: a slow solution you trust, used to check a fast one
  • stress test: many random small inputs, comparing the two

Step by step:

  1. read the limits and write down the edge cases they allow
  2. work out the right answers for those by hand, and test them
  3. write a brute-force oracle
  4. run both on 1,000 random small inputs; stop at the first difference and study it

Binary search is the classic place where edge cases bite: watch lo and hi on an ordinary case, then ask what happens when the target is the first item, the last item, or missing:

Why random small inputs find bugs. A bug needs a particular shape of input to show itself. Hand-made examples cover the shapes you thought of; a thousand random lists of up to four numbers between −5 and 5 cover nearly every shape that small, including the ones you didn't think of. Keeping them small keeps the brute force fast and the failing case short enough to read.

What it costs, by counting. 1,000 random lists of at most 4 numbers cost the O(n³) brute force at most 20 additions each, about 20,000 in all: a fraction of a second, against an evening of hunting the bug by hand.

import random

def brute_max_sum(nums):                # every stretch, slowly but surely
    return max(sum(nums[i:j]) for i in range(len(nums)) for j in range(i + 1, len(nums) + 1))

def fast_max_sum(nums):                 # one pass, with a bug hidden in it
    best = cur = 0
    for x in nums:
        cur = max(x, cur + x)
        best = max(best, cur)
    return best

print(fast_max_sum([2, -1, 3]), brute_max_sum([2, -1, 3]))   # the example passes
random.seed(7)
for _ in range(1000):
    nums = [random.randint(-5, 5) for _ in range(random.randint(1, 4))]
    if fast_max_sum(nums) != brute_max_sum(nums):
        print("they differ on", nums, fast_max_sum(nums), brute_max_sum(nums))
        break

Mistakes and edge cases.

  • testing only the given example: it was chosen to explain the problem, not to break your code
  • starting a best value at 0: wrong as soon as every answer can be negative; start from the first item
  • an oracle with the same idea: if the brute force shares the bug, the two agree and prove nothing; keep the oracle as simple as possible
  • random inputs that are too big: a failing list of 1,000 numbers is hard to read; shrink the sizes until the failure is tiny

Quiz

  1. Which is the most useful extra test for "return the biggest sum of a stretch of the list"?

    • A list where every number is negative
    • Another list of positive numbers
    • The same example again
  2. What does this print?

    def fast_max_sum(nums):
        best = cur = 0
        for x in nums:
            cur = max(x, cur + x)
            best = max(best, cur)
        return best
    
    print(fast_max_sum([-3, -1, -2]))
  3. Why keep random test inputs small?

    • The brute force stays fast and a failing case is short enough to understand
    • Big inputs never show bugs
    • Python can't make big random lists
  4. The fast version and the brute force agree on 1,000 random inputs, but both share the same wrong idea. What does the test prove?

    • Nothing about correctness: the oracle must be simple enough to trust on its own
    • That both are right
    • That the fast one is faster
  5. This binary search misses a target that is the last item, like find([2, 5, 8], 8). Which line is wrong?

    def find(items, target):
        lo, hi = 0, len(items) - 1
        while lo < hi:
            mid = (lo + hi) // 2
            if items[mid] == target:
                return mid
            if items[mid] < target:
                lo = mid + 1
            else:
                hi = mid - 1
        return -1

Coding tasks

  1. The starter's max_stretch_sum(nums) passes the example [2, -1, 3] (4) but fails an edge case. Fix it: return the biggest sum of a non-empty stretch of numbers next to each other. nums has at least one number.

  2. Write find_first_differ(fast, slow, inputs): call both functions on each input in order and return the first input where their answers differ, or None if they always agree. This is the heart of a stress test.