Applied Checkpoint: Validate Study Records
Unit ID: M04-U09 Estimated active time: 45-60 minutes
Supplied data
records = [
{"id": "R1", "hours": 8},
{"id": "R2", "hours": -2},
{"id": "R3", "hours": "6"},
{"id": " ", "hours": 4},
{"id": "R5", "hours": 40},
{"id": "R6", "hours": True},
{"hours": 12},
]
Use only these fictional records.
Rules
A record is accepted only when:
idexists, has exact typestr, and contains non-whitespace text;hoursexists, has exact typeint, and is from 0 through 40 inclusive.
Required outputs
Create:
accepted_records: copies of valid records;rejected_records: dictionaries containing the original position, a safe display ID, and a list of reasons;accepted_hours_total;- accepted and rejected counts; and
has_rejectionsboolean.
Do not overwrite or remove items from records.
Required process
- Write the plain-language algorithm before coding.
- Initialise all output state before the loop.
- Use
enumerate(records, start=1). - Create a fresh
reasonslist for each record. - Validate ID and hours separately.
- Use
elifso range comparison occurs only after integer type passes. - Append a record copy only when
reasonsis empty. - Otherwise, append all reasons to rejected output.
- Calculate summary values after or during the loop without double counting.
Expected result
- Accepted IDs:
R1,R5 - Accepted count:
2 - Rejected count:
5 - Accepted hours total:
48 - Rejections exist:
True
Required assertions
Assert the expected counts and total. Also assert that records still equals a preserved source copy.
Self-review rubric
| Criterion | Not yet | Meets |
|---|---|---|
| Algorithm | Coding begins without clear rules and outputs | Inputs, outputs, repetition, decisions, and boundaries are stated first |
| Type and range | Booleans or numeric text are accepted accidentally | Exact integer type and inclusive range are enforced |
| Rejection evidence | Invalid records disappear or only one generic reason appears | Every rejection keeps position, display ID, and all relevant reasons |
| Source preservation | Source records are changed or removed | Accepted records are copies and source equality is asserted |
| State | Counts or totals depend on stale notebook values | State is initialised once and updated in the correct branch |
| Edge cases | Missing keys or spaces-only IDs fail unexpectedly | Missing, blank, boundary, string, boolean, and negative cases are handled |
| Reproducibility | Clean rerun fails | Restart-and-run-all passes all assertions |
