Case File: Your First Audit
The finale brings everything together: read a file, loop, count with a dictionary, apply a rule with exact edges, write a function, and use dates. Check the delivery promise of the company: for each delivery work out the days it took (leap days and year changes included), mark it ok, late or pending, count each, find the slowest, average the finished ones, and say fail or ok. A clean audit must say ok. The deliveries are generated for you alone.
Sabi Logistics Β· Python 9
Your First Audit
The customer says that we missed our delivery promise and wants a signed statement of how many deliveries were late
Halima Operations manager Β· Phone call
Halima. A customer says that we missed our delivery promise this month and wants a signed statement of how many deliveries were late. I will not sign anything that I cannot explain. We promise delivery within a number of days of sending.
I have the date each parcel was sent and the date it was delivered (or nothing yet, if it is still on the road). Please audit it: the days each delivery took, which are ok, which are late and which are pending, how many of each, the slowest delivery, the average of the finished ones, and one word at the end: ok or fail. And if nothing is late, the audit must clearly say ok.
The brief
What you need to know first.
- Dates are not text.
"2026-10-04"is only text until you convert it:date.fromisoformat("2026-10-04")(afterfrom datetime import date). Subtracting two dates gives a difference, and.daysis its length in whole days:(date.fromisoformat(b) - date.fromisoformat(a)).days. Python knows about months, years and leap days, so you never count by hand: 2028-02-28 to 2028-03-01 is 2 days, and 2026-12-31 to 2027-01-03 is 3. - Three statuses. A delivery whose
deliveredisNoneis pending: it is neither ok nor late yet, and itsdaysisNone. Otherwisedaysis the days between sent and delivered, and the status is late whendaysis more thansla_days(exactly the SLA is ok), else ok. - The audit result.
rows(id, days, status in order),counts(how many of each status that occurs),worst_id(the id with the most days among the finished ones, the first on a tie,Noneif none finished),mean_days(the average days of the finished ones, 3 decimals, 0 if none), andstatus:"fail"if any delivery is late, otherwise"ok". - A clean audit must say ok. A report that can only report problems is not an audit: an empty file, or one without late deliveries, gives the status
"ok", with the counts that were found. - Fill in
days_between,audit(req), thenresultandn_pending. The checks runauditon files that you cannot see: a clean one, one with a leap day, a year change and a pending delivery, and an empty one.
The lab gives you the data for this case, invented and generated for you alone.
Preparing your dataβ¦
Halima: βThe audit says fail: five late and two pending. Can I sign that nothing else was late?β
π The debrief unlocks when you close the case. Finish the lab above.
The debrief
Halima: βSo I can sign what was checked, but not what is still open.β
What an experienced person would do next. Sign only what the data supports: βof N deliveries, X were late; Y were still pending on the date of the report and are not included in either group.β Add the date of the report, so that pending deliveries can be rechecked later. Keep the program and the input file with the statement, so that anybody can run it again and get the same numbers. Test with your own small files, where you worked the answer out by hand, including the edges (exactly the SLA, a leap day, a year change, an empty file). And notice what you have learned: every case file in the other courses is this same shape, read a file, apply written rules with exact edges, count, and report honestly, in a larger setting.
The trap. Counting a pending delivery as ok, and an audit that has no way to say that everything is fine.
Write the first audit note
Write the statement for the operations manager and the customer: the rule you checked (the delivery promise in days, exactly the promise is ok), how many deliveries were examined, how many were ok, late and pending, the slowest delivery and the average days of the finished ones, the overall status, what the pending deliveries mean (not yet known, to be rechecked), the date of the report, and what you can honestly sign.
π§ͺ Want to practise this on your own computer? See how to set up your lab →