Problem 134165 · hard · Level 01 Prerequisites & Setup

Which Release Is Newer?

conditionals · strings · loops · functions

Software releases are labelled with version strings such as "2.10.1" or "3.0-beta.2". Write newer(a, b) that returns 1 if version a is newer than b, -1 if it is older, and 0 if they rank the same.

A version is a core of one or more dot-separated whole numbers, optionally followed by - and a tag of one or more dot-separated identifiers (made of letters and digits).

  1. Compare the cores number by number from the left, by numeric value ("10" > "9", "007" == "7"). A missing number counts as 0, so "1.2" ranks the same as "1.2.0".
  2. If the cores tie, a version without a tag is newer than one with a tag.
  3. If both have tags, compare identifiers from the left:
    • two all-digit identifiers compare by numeric value;
    • two identifiers that are not all digits compare as strings with < and >;
    • an all-digit identifier is older than one that is not all digits.
  4. If one tag runs out first and all identifiers so far tied, the longer tag is newer.

Examples

Input:  a = "1.10", b = "1.9.9"
Output: 1

Input:  a = "2.0.0-rc.1", b = "2.0"
Output: -1

Input:  a = "1.0-alpha.10", b = "1.0-alpha.9"
Output: 1

Constraints

  • 1 <= len(a), len(b) <= 100
  • the strings follow the format above (no empty parts, at most one -)

Goals

  • Split a string into structured parts
  • Compare piece by piece with the right rule for each kind of piece
  • Handle missing pieces and ties precisely
Starting Python…