๐Ÿ“ฆ Ops

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
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.