Coordinates, With Feeling
Stardate 55135.7 ยท UES Magnanimous
Briefing
The contact is sending its position as a pair of numbers in a string, comma separated, with whatever spacing its transmitter felt like. "12.5, -3.25" is a position. "12.5,-3.25" is the same position. "twelve, three" is not a position, and neither is "12.5" on its own, and Lt. Skree wants those rejected rather than guessed at.
The Captain has asked whether "rejected" means the ship will crash. It means the function returns nothing, and the caller decides. The Captain has said "good" and gone back to looking at the viewscreen, where the contact is now visible as a dot.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขA well-formed pair with a space after the comma
- โขThe same pair with no spaces at all
- โขWords instead of numbers are rejected
- โขA single number with no comma is rejected
- โขExtra whitespace around either number is fine
Objective
Write parse_coords(text): return a tuple (x, y) of floats
parsed from "x, y", tolerating any whitespace around the numbers. Return
None if the text is not exactly two numbers separated by one comma.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Write parse_coords(text: &str) -> Option<(f64, f64)>: parse
"x, y" into a pair, tolerating any whitespace around the numbers. Return
None if the text is not exactly two numbers separated by one comma.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Split on the comma; if you do not get exactly two parts, reject. Trim each part and try to convert it to a number; if either fails, reject. Python: catch the ValueError. Rust: parse().ok() and the ? operator inside a function returning Option.
Debrief
The contact is at 12.5 by minus 3.25, relative, and closing very slowly. Lt. Skree has confirmed it is a ship. Skree has also confirmed, after a pause, that its transponder reads UES Magnanimous. Commander Raghunathan has come back to the bridge.