Cargo Priorities
Stardate 55149.2 ยท UES Magnanimous
Briefing
Nobody has said the words "abandon ship". Chief T'Kala has, however, asked for the cargo loading order to be worked out "as an exercise", and Commander Raghunathan has asked for it "today", and neither of them is making eye contact with the other.
Each crate has a name, a priority from 1 to 5, and a mass. Load the highest priority first. Among equal priorities, load the lightest first, so more of it fits. The result is just the names, in loading order. It is a sort with two keys, and the second key runs the opposite way to the first, which is the bit that catches people.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขHigher priority loads before lower
- โขEqual priority: lighter loads first
- โขBoth rules together on a mixed manifest
- โขA single crate
- โขAn empty manifest
Objective
Write loading_order(crates): crates is a list of dicts with
keys "name", "priority" (higher loads first) and
"mass" (among equal priorities, lighter loads first). Return the list of
names in loading order.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Keep the struct as given. Write loading_order(crates: &[Crate]) ->
Vec<String>: names in loading order, priority descending, and among equal
priorities mass ascending.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Sort by a compound key. Python: key=lambda c: (-c['priority'], c['mass']), negating the priority so it runs descending. Rust: sort_by(|a, b| b.priority.cmp(&a.priority).then(a.mass.cmp(&b.mass))).
Debrief
Medical first, then the light priority-fours, then everything else. Chief T'Kala has printed the list and put it in the pocket of her jacket. She has not said why. Gerald is priority five, mass four, and loads second.