Compare Version Numbers as Tuples

Medium ⏱ 12 min 54% acceptance ★★★★☆ 4.3
Implement is_newer_version(v1, v2), where each version is a tuple of ints like (1, 4, 2) for major.minor.patch. Return True if v1 is strictly newer than v2, relying directly on Python's built-in lexicographic tuple comparison — no manual field-by-field if/elif chain.

Examples

Example 1
Input
is_newer_version((1, 4, 2), (1, 3, 9))
Output
True
Explanation

Minor version 4 beats 3, so v1 is newer regardless of the patch numbers.

Example 2
Input
is_newer_version((1, 4), (1, 4, 0))
Output
False
Explanation

(1, 4) is a strict prefix of (1, 4, 0) and Python treats the shorter prefix as smaller, so v1 is not newer.

Constraints

  • Tuples may have differing lengths.

Topics

TuplesComparison

Companies

MicrosoftAtlassian

Hints

Hint 1

Python compares tuples element by element and, on a matching prefix, the shorter tuple counts as smaller.

Hint 2

v1 > v2 does exactly this comparison.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.