Applied Checkpoint: Repair an Hours-Processing Program
Unit ID: M06-U08 Estimated active time: 50-70 minutes
Broken program
The supplied program assumes every hours value is numeric text, stops at the first bad record, silently accepts some unsuitable values, and has no focused tests.
Use these fictional records:
records = [
{"id": "R1", "hours": " 8 "},
{"id": "R2", "hours": 12},
{"id": "R3", "hours": "six"},
{"id": "R4", "hours": -2},
{"id": "R5", "hours": True},
{"id": "R6"},
{"id": "R7", "hours": "40"},
]
Required diagnosis
Before repair, record:
- expected accepted records;
- the first actual failure and traceback;
- the smallest reproducing input;
- every violated assumption you find; and
- why a broad catch or silent conversion would be unsafe.
Required repair
Create parse_hours(raw_hours) with this contract:
- accept exact integers and integer text;
- strip outside text whitespace;
- reject booleans, floats,
None, malformed text, and empty text; - require 0 through 40 inclusive;
- raise
TypeErrorfor unsupported types; - raise
ValueErrorfor malformed or out-of-range values.
Create process_records(records) that:
- preserves the source list and dictionaries;
- continues after expected
KeyError,TypeError, orValueErrorper record; - returns accepted record copies, rejected details, and accepted-hours total;
- records position, safe ID, exception type, and reason for each rejection.
Expected outcome
- Accepted IDs:
R1,R2,R7 - Accepted hours:
8,12,40 - Accepted total:
60 - Rejected count:
4
Required tests
Test:
- normal integer and numeric text;
- 0 and 40;
- -1 and 41;
True,None, and3.5;"six","8.5", and empty text;- missing hours key in record processing;
- source preservation; and
- expected accepted IDs, rejected count, and total.
Self-review rubric
| Criterion | Not yet | Meets |
|---|---|---|
| Diagnosis | Code is changed before evidence is recorded | Expected, actual, traceback, minimal input, and violated assumptions are documented |
| Validation | Values are silently coerced or booleans pass | Type, text, and range rules match the contract |
| Exceptions | Failures are swallowed or caught broadly | Specific errors have useful messages and expected catches are narrow |
| Processing | First invalid record stops the workflow | All records produce accepted or rejected evidence |
| Source safety | Input records are changed | Source equality is tested and preserved |
| Tests | Only one successful example is tried | Normal, boundary, invalid, missing, and integration cases pass |
| Explanation | Repair is unexplained | Root cause, repair, and residual limits are stated clearly |
