Reading the Traceback
Stardate 55161.7 ยท UES Magnanimous
Briefing
Welcome to Sickbay, which on this ship is where broken code goes to be diagnosed rather than replaced. Commander Raghunathan's rule is that you do not rewrite a routine until you can say what was wrong with it, because otherwise you will write the same thing wrong again.
The patient is a median function. It has more than one thing wrong with it. Run the diagnostics, read what ARCHIE says about each failing objective, and fix that. Then run again. Lt. Skree has already diagnosed it from across the room, but has been asked, gently, to let you do it.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขThe median of an odd-length list is the middle value
- โขThe median of an even-length list is the mean of the two middle values, 2.5 not 2
- โขThe caller's list is not reordered
- โขAn empty list gives None, not a crash
- โขA single value is its own median
Objective
Fix median(values) so it returns the median of a list of numbers: the
middle value when the count is odd, the mean of the two middle values when even, and
None for an empty list. It must not modify the caller's list.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Fix median(values: &[f64]) -> Option<f64>: the middle value
when the count is odd, the mean of the two middle values when even, and
None for an empty slice. Several things are wrong with it; the failing
objectives will tell you which.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Sort a copy. For odd length the middle index is n // 2. For even, average the two middle values with real division. Check for empty first. Each failing objective points at one of those.
Debrief
Four faults: it sorted the caller's list in place, it picked the element after the middle, it used integer division on the even case, and it fell over on an empty list. Lt. Skree says that was three faults and a symptom. Commander Raghunathan says that is what four faults looks like from across the room.