CASE STUDY 02 · PYTHON

Intermediate Dataclass + CSV + Analytics

Student Attendance Analyzer

Record each class consistently, calculate eligibility, identify shortages and turn percentages into specific recovery or safe-absence guidance.

01 · PROBLEM DEFINITION

A percentage should lead to an actionable answer

A coordinator needs more than attended/conducted values. The system must update every student consistently when a class occurs, identify students below 75%, calculate how many consecutive future classes they must attend, and tell eligible students how many future absences they can currently tolerate.

Roster

Maintain unique student IDs and names.

Class event

Increase conducted for everyone and attended for present IDs.

Advice

Convert percentages into recovery or safety counts.

Policy boundary: The example threshold is 75%. Institutional regulations may define subject-wise attendance, condonation and exceptions differently.
02 · DATA RULES & INVARIANTS

Attendance is a coordinated state change

  1. Student ID is non-empty and unique.
  2. Name is non-empty.
  3. 0 ≤ attended ≤ conducted.
  4. Recording one class increments conducted for every enrolled student exactly once.
  5. Only known present IDs receive an attended increment.
  6. Unknown IDs reject the complete class update rather than creating partial data.

The last rule makes the operation atomic at the application level: either the full roster is updated consistently or no attendance count changes.

03 · OBJECT MODEL

Computed properties keep derived state fresh

StudentAttendance(student_id, name, attended, conducted)
percentage = attended × 100 / conducted
status = Eligible when percentage ≥ 75

Percentage and status are properties, not stored columns. Storing them would create redundant values that could disagree after an update. The raw counts remain the source of truth.

The report sorts by percentage ascending and then ID, placing the students needing attention first while producing deterministic ties.

04 · RECOVERY & SAFE-ABSENCE MATHEMATICS

Solve inequalities, then round in the correct direction

Classes needed to recover

If a student attends every next class, find smallest integer x such that:

(attended + x)/(conducted + x) ≥ r
x ≥ (r×conducted − attended)/(1−r)

Use ceiling because a fraction of a class cannot be attended and the requirement is a minimum.

Currently safe absences

Find largest whole x such that attended/(conducted+x) ≥ r. Rearrangement gives x ≤ attended/r − conducted; use floor. This is a snapshot, not permission to ignore later policy changes.

Example: 18/25=72%. At r=.75, recovery x≥(18.75−18)/.25=3, so attending the next three gives 21/28=75%.
05 · PERSISTENCE WORKFLOW

Write the whole coordinated update safely

Load CSV
Validate every row
Validate present IDs
Update all records
Write temporary file
Replace original

A class changes many rows, so simple append is unsuitable. The program writes a complete temporary CSV and then replaces the original. This prevents a normal mid-write failure from leaving a half-written destination, though stronger durability still requires database transactions and filesystem-specific guarantees.

06 · COMPLETE IMPLEMENTATION

Runnable Python program

programs/attendance-analyzer.py
Open Compiler
Loading source…

Run locally for persistent CSV behavior. The program uses only Python’s standard library and validates stored counts before building objects.

07 · INTERACTIVE PROGRAM TRACE

Trace one class event

  1. Read and validate roster.
  2. Validate the complete set before mutation.
  3. Record that one class occurred.
  4. Apply presence.
  5. Preserve 0≤attended≤conducted.
  6. Create replacement data.
  7. Commit the class event.
  8. Generate shortage advice.
  9. Show priority order.
Current state

Press Next to begin.

08 · TEST STRATEGY

Protect boundaries and coordinated updates

Threshold equality
15/20 must be Eligible at exactly 75%, not Shortage.
Zero classes
0/0 displays 0% under this reporting convention without division by zero.
Recovery formula
18/25 must require three consecutive attended classes to reach 21/28=75%.
Unknown present ID
Include one invalid ID; no student count and no file content may change.
Duplicate roster ID
A second student with an existing ID must be rejected.
Damaged CSV row
Reject attended greater than conducted and report its row number.
09 · COMPLEXITY & LIMITATIONS

Each class update scans the roster

OperationTimeSpace
Load n studentsO(n)O(n)
Validate present setO(n+p)O(n+p)
Record class/saveO(n)O(n)
Sorted reportO(n log n)O(n)

The model stores cumulative counts, not a date-wise audit trail. A production system needs session records, course/subject separation, authentication, correction history and role-based approval.

10 · PRACTICE & EXTENSIONS

Check the formulas

Why does recovery use ceiling?

Why not store percentage in CSV?

Extensions

  1. Add subject-wise sessions and date-wise history.
  2. Support authorized corrections with an audit log.
  3. Generate weekly shortage alerts without exposing records publicly.
  4. Import a roster and detect duplicate IDs.
  5. Move coordinated writes to SQLite transactions.
11 · INTERVIEW PREPARATION

Explain derived data and atomicity

Why use properties?

Percentage and status always reflect current counts and cannot become stale stored fields.

Why validate all IDs first?

Discovering an unknown ID after partial mutation would leave students updated inconsistently.

Why temporary-file replacement?

A multi-row update should not overwrite the original until a complete replacement has been written.

What does this model not audit?

It lacks individual session dates and presence history, so it cannot prove which specific classes were attended.

12 · KEY TAKEAWAY

An analyzer should explain the next action

Python properties, sets, CSV, exceptions and file replacement support the system, while inequality solving turns a raw percentage into useful recovery guidance. Correctness depends on updating the whole roster as one event.