Heading: Validated
Stardate 55140.2 ยท UES Magnanimous
Briefing
Welcome to Tactical, which on this ship means the station that says no. Chief T'Kala runs it, and she has a list.
Item one on the list: the helm accepts any heading a person types. Yesterday it accepted "left". Last week it accepted 400, which it treated as 40, and -12, which it treated as a mood. A heading is a whole number from 0 to 359, and the Chief wants text that is anything else to be refused: not corrected, not guessed at, refused, so the person at the helm has to try again. Whitespace around a valid number is fine. Ensign Tannenbaum typed most of the examples.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขA valid heading is accepted
- โขWhitespace around a valid heading is fine
- โขWords are refused
- โข360 and above are refused, not wrapped
- โขNegative numbers are refused
Objective
Write parse_heading(text): return the heading as an int if
text is a whole number from 0 to 359 (whitespace around it allowed).
Otherwise return None. Do not wrap or clamp: "400" is refused,
not turned into 40.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Write parse_heading(text: &str) -> Option<u16>: the heading
if text is a whole number from 0 to 359 (whitespace around it allowed).
Otherwise None. Do not wrap or clamp: "400" is refused, not
turned into 40.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Trim, then try to convert to a whole number; if that fails, refuse. Then check the range 0 to 359 and refuse anything outside it. Rust's parse::<u16>() already refuses negatives, and .ok()? makes the whole thing three lines.
Debrief
The helm refuses "left". It refuses 400. It refuses -12, and it refuses "12 degrees", and Ensign Tannenbaum has stopped typing things into it to see what happens. The other Magnanimous is holding station at heading 180, exactly facing us. Chief T'Kala has quietly brought the shields up.