Case File: The Depot Tally
A week of parcels, each handled by one depot, each with a fee. Build the tally that every manager asks for: how many parcels and how much fee for each depot, and which depot was busiest, with a fair rule for ties. Learn the dictionary and the count-by-key pattern that appears in almost every case file. The parcels are generated for you alone.
Sabi Logistics · Python 3
The Depot Tally
The depot managers argue about who is the busiest, and the bonus depends on it
Halima Operations manager ·
Halima. Each depot manager says that their depot was the busiest this week, and the bonus goes to the busiest. I have the list of the week’s parcels: which depot handled it and the fee that was charged.
Please give me, for each depot, the number of parcels and the total fee, and name the busiest depot (the one with most parcels). If two depots are tied, I want a rule that I can explain to them: the one whose name comes first in the alphabet. And do not crash if there are no parcels.
The brief
What you need to know first.
- A dictionary stores values under names:
counts = {}is an empty one;counts["Kano"] = 3stores 3 under “Kano”;counts["Kano"]reads it back. Reading a name that is not there is an error (KeyError), so you ask politely:counts.get("Kano", 0)gives the value, or 0 if the name is missing. - Count by key. This is the pattern:
counts[depot] = counts.get(depot, 0) + 1for each parcel. The first time a depot appears,getgives 0 and the count becomes 1. Totals are the same with+ p["fee"]instead of+ 1. You will write this line in nearly every case file. - Looping over a dictionary.
for name in counts:gives the names;counts[name]the values;sorted(counts)the names in alphabetical order. - The busiest, with a fair tie rule. Go through the names in sorted order, keep the best name so far, and replace it only when a count is strictly larger: because you meet the names alphabetically and only a larger count replaces the best, a tie keeps the name that sorts first. With no parcels there is no busiest:
None. - Fill in
tally(req)returning{"counts", "fees", "busiest"}, then the variables. The checks runtallyon lists that you cannot see: a tie between two depots, and an empty list.
The lab gives you the data for this case, invented and generated for you alone.
Preparing your data…
Halima: “Abuja and Kano are tied, and the tie rule gave Abuja. Kano says that fees should count, not parcels. Who is right?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Halima: “So the rule has to be chosen before the results are known.”
What an experienced person would do next. Write down the rule (parcels, fees, or a mix), the tie-break and the period before you look at the numbers, so that nobody can say that the rule was picked to favour one depot. Show both measures side by side when they disagree. And keep the dictionary pattern in your head: group, then count or add answers a very large share of the questions that people ask about a list.
The trap. counts[depot] += 1 for a depot that is not in the dictionary yet: it fails with a KeyError. Use get with a default.
Write the tally note
Write the note for the operations manager: how the tally was done (a count and a total for each depot with a dictionary), the numbers for each depot, which depot was busiest and by how much, the tie rule and why it is fair (the name that sorts first, decided before looking), what happens with no parcels, and a sentence on why the measure should be agreed before the numbers are known (parcels or fees).
🧪 Want to practise this on your own computer? See how to set up your lab →