One Hundred and Forty Percent
Stardate 55109.0 ยท UES Magnanimous
Briefing
The shield status display on the bridge currently reads 140%. This is not a real number. Commander Raghunathan has explained, twice, that the emitters physically cannot exceed full charge and that the display is simply not being clamped. Ensign Tannenbaum has asked whether 140% could be "a stretch goal".
Separately, during the last drill the same display read -20%, which briefly convinced two junior officers that the ship had negative shields, whatever those would be. The Commander would like both ends of the range dealt with, today.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขA negative reading is clamped to 0
- โขZero stays zero
- โขA normal reading passes through unchanged
- โขExactly 100 stays 100
- โขAn overcharged reading is clamped to 100
Objective
Write clamp_shields(percent): return the value limited to the range 0 to
100 inclusive. Values below 0 become 0, values above 100 become 100, everything else is
returned unchanged.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Write clamp_shields(percent: i32) -> i32: return the value limited to
the range 0 to 100 inclusive. Values below 0 become 0, values above 100 become 100,
everything else is returned unchanged.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Two comparisons, or a pair of min/max calls. Rust has .clamp(0, 100), which is one method and worth knowing.
Debrief
The shield display now reads a number between 0 and 100, and Commander Raghunathan has removed the sticky note that said "IGNORE THIS" from the corner of it. She has kept the sticky note. She says it may be needed elsewhere.