Chapter 140
HomeWhen your solution fails a test but you can't find the bug, stress test: write a slow brute-force, generate random small inputs, and compare outputs. When they differ, you have a minimal failing case.
# gen.py â generates random test case import random, sys n = random.randint(1, 10) arr = [random.randint(1, 100) for _ in range(n)] print(n) print(*arr) # Then: while true; do python gen.py > in.txt # ./fast_sol < in.txt > out1.txt # ./brute_sol < in.txt > out2.txt # diff out1.txt out2.txt || break; done # When diff fails â you've found your bug!
ðĄ Keep inputs small
The brute-force should be O(N!) or O(2áīš). Keep N âĪ 10 for stress testing. The fast solution handles arbitrary N â you're checking correctness, not performance.