๐Ÿฉบ Sickbay

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