Exercise 02 — Find the Bugs¶
This module (codebase.py) is a data processing pipeline that reads sales records from a CSV, filters them by date, summarizes revenue per category, and writes results to a new file.
It looks reasonable. It has docstrings. It runs without crashing on simple inputs.
It has 4 bugs. Find and fix all of them.
Rules¶
- Read the code carefully before running anything.
- For each bug, write down:
- Which function contains the bug
- What the bug is
- What the correct behavior should be
- Your fix
- After fixing all 4, run the tests in
tests/test_codebase.pyto verify.
Hints (read one at a time, only if stuck)¶
Hint 1: Think about resources
One function opens something but never closes it. What happens if this function is called many times?Hint 2: Think about indexing
The paginate function claims to be 1-indexed. Is it really?Hint 3: Think about whitespace
CSV data from the real world is messy. What if there are trailing spaces in a column value?Hint 4: Think about boundaries
The docstring for filter_by_date says the range is [start, end). Read the code. Does the implementation match that contract?What you are practicing¶
- Reading code critically (not trusting that it works just because it looks clean)
- Identifying common bug patterns: resource leaks, off-by-one errors, missing input sanitization, contract violations
- Writing fixes and verifying them with tests