The Loudest Three
Stardate 55147.7 ยท UES Magnanimous
Briefing
The pattern in the other ship's log is a set of spikes. Lt. Skree wants the largest few readings from any stretch of the log, largest first, and wants the request to be one expression, because Skree has been reading about iterator chains and has decided they are "the correct shape for thoughts".
The Captain, passing, asked whether the spikes were dangerous. Skree said they were "nineteen years old". The Captain said that was not an answer. Skree agreed that it was not, and went back to the console.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขThe three largest, largest first
- โขTies are kept, not collapsed
- โขFewer readings than asked for: all of them, largest first
- โขAsking for zero gives nothing
- โขThe input is not modified
Objective
Write top_n(readings, n): return a new list of the n largest
readings in descending order. If there are fewer than n, return all of them
sorted descending. Do not modify readings.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Write top_n(readings: &[i32], n: usize) -> Vec<i32>: the
n largest readings in descending order. If there are fewer than
n, all of them sorted descending. The input slice is borrowed, so it cannot
be modified; that objective is one Rust checks for you.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Sort a copy in descending order and take the first n. Python: sorted(readings, reverse=True)[:n]. Rust: clone into a Vec, sort_unstable_by(|a, b| b.cmp(a)), truncate(n). The last objective is why it must be a copy.
Debrief
Three spikes, on three consecutive stardates, each larger than the last, ending the day before the log stops. Skree has put the numbers next to ours. Ours has the first spike. Today. Skree has asked the Captain to come back to the console.