Editor

Compare Version Numbers

#56 · Parsing · Intermediate · 60 XP · about 10 min

A version number is a list of whole numbers separated by dots, like "2.14.0". Write compare_versions(a, b) that returns -1 if version a is older than b, 1 if it is newer, and 0 if they are the same version.

Compare the parts from left to right as numbers, so 10 is bigger than 9 and "007" is the same as "7". The first part that differs decides. A missing part counts as 0, which makes "1.2" and "1.2.0.0" the same version.

Examples

compare_versions("1.10", "1.9")     # returns 1
compare_versions("1.2", "1.2.0.0")  # returns 0
compare_versions("0.9.9", "1.0")    # returns -1

Constraints

  • Each version has 1 to 50 parts.
  • Each part has 1 to 10 digits, with no signs and no letters.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.