beginner

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.

45 min

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"] = 3 stores 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) + 1 for each parcel. The first time a depot appears, get gives 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 run tally on 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…

🔒 The debrief unlocks when you close the case. Finish the lab above.

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).

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for Case File: The Depot Tally. What would you like to know?
📝

Knowledge Check

1 of 3

What does counts.get("Kano", 0) give when "Kano" is not in the dictionary?

⚑ Capture The Flag

Report a problem

About: /lessons/case-the-depot-tally