Back to Practice
#0229
Compare Semantic Versions
MediumPython25 min20 XP
Problem
Compare version strings like "1.2.0" and "1.10". Return -1, 0, or 1.
Why This Matters
Version comparison is a real parsing task where string comparison gives wrong answers, such as 1.10 vs 1.2.
Function Signature
def compare_versions(a, b):
Examples
Example 1
Inputa = "1.10", b = "1.2.9"
Output1
The second part 10 is greater than 2, so 1.10 is newer.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Prefer readable Python before micro-optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputa = "1.10", b = "1.2.9"
Expected1
The second part 10 is greater than 2, so 1.10 is newer.
Hidden Test Categories
equal with trailing zerosshorter left versionlarger patch version