The Most Requested Item
Stardate 55125.6 ยท UES Magnanimous
Briefing
Chief T'Kala would like to know which item is requisitioned most often, so Stores can keep more of it. The naive answer is "plasma couplings", because of the Tannenbaum incident, but the Chief wants the count done properly, over the real log, with the Ensign's eleven duplicates already removed.
Where two items tie, she wants the one that comes first alphabetically, so the answer is stable and nobody argues. Where the log is empty, she wants nothing, rather than a guess. Commander Raghunathan has predicted the answer will be "coffee filters" and has put money on it.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขThe most frequent item in a mixed log
- โขA tie goes to the alphabetically first item
- โขA single-entry log
- โขEvery entry the same
- โขAn empty log: nothing, not a guess
Objective
Write most_requested(log): log is a list of item names.
Return the name that appears most often. If several tie, return the alphabetically
first of them. Return None for an empty log.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Write most_requested(log: &[&str]) -> Option<String>: the
name that appears most often. If several tie, the alphabetically first of them.
None for an empty log.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Count each item into a dictionary or map first, then pick the winner. For the tie rule, compare on (count, name) rather than count alone: highest count, and among equals, the smallest name.
Debrief
Coffee filters. Commander Raghunathan has collected. Stores has ordered a great many coffee filters, and the Captain, on hearing this, said "good" in a tone that closed the subject.