Which Ship Is Newer?
Stardate 55153.9 ยท UES Magnanimous
Briefing
The other computer runs Magnanimous system software version 1.10.3. Ours runs 1.9.7. The comparison routine says ours is newer, because it compares the version strings as text, and as text "1.9" comes after "1.10". As numbers, it does not. Version numbers are three integers with dots between, and they compare part by part.
Commander Raghunathan wants a version type that parses the string, compares
numerically, and prints itself back out. In Python that means the comparison methods. In
Rust it means implementing Ord, after which sorting, min and max all work on
your type for free. Lt. Skree has asked whether "newer" means "better". The Commander
has said "not today".
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขParses major, minor and patch from a string
- โข1.10.0 is newer than 1.9.7, because parts compare as numbers
- โขEqual versions compare equal
- โขRenders back to the same string
- โขA list of versions sorts correctly
Objective
Write a class Version: Version("1.10.3") parses three
integers major, minor, patch. Instances compare
numerically part by part (==, <, and the rest), so
Version("1.10.0") > Version("1.9.7"). str(v) gives back
"1.10.3".
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Keep the struct as given. Write Version::parse(text: &str) ->
Version that reads three integers from "1.10.3", implement
Display so it prints back as "1.10.3", and make it ordered
(PartialEq, Eq, PartialOrd, Ord) so
versions compare numerically part by part. Deriving works if the fields are in the right
order.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Split on the dots and convert each part to an integer. Then compare the tuple of three numbers, which Python and Rust both do part by part. Python: implement __eq__ and __lt__ and use functools.total_ordering. Rust: derive or implement Ord on a struct of three integers.
Debrief
Theirs is newer. By one release. Commander Raghunathan has pulled up the release notes for 1.10.3, which are the same as ours plus one line: "pressure|flow mismatch: resolved". She has looked at the date on it for a while.
The Anomaly is complete. Deep Space is open, and Engineering with it. Whatever the other Magnanimous is, tomorrow's stardate is in both logs.