v9.1.1
Menu

Highest Semver Matching a Caret Range

mediumPROContainer Images & BuildsSystems & InfrastructureML Systems & Infrastructure 1

Problem Statement

Resolve a base image tag: given available semantic-version tags and a caret constraint like ^1.2.3, pick the highest version that satisfies it.

Background

A caret range ^MAJOR.MINOR.PATCH allows versions >= MAJOR.MINOR.PATCH and < (MAJOR+1).0.0 (for MAJOR >= 1). Versions are compared component-wise as integer triples. Return the highest available version in range, or None if none qualify.

Your Task

def resolve_caret(versions, constraint):
  • versions: list of "X.Y.Z" strings.
  • constraint: "^X.Y.Z".
  • Return the highest matching version string, or None.

Input Format

  • versions (list of str), constraint (str).

Output Format

  • A version string or None.

Sample

print(resolve_caret(["1.2.0", "1.4.1", "2.0.0"], "^1.2.3"))

Output:

1.4.1

Example

Input
print(resolve_caret(["1.2.0", "1.4.1", "2.0.0"], "^1.2.3"))
Output
1.4.1
Reasoning
  • Parse the constraint ^1.2.3 to establish the valid version range. The lower bound is inclusive at (1,2,3)(1, 2, 3), and since the major version is ≥1\ge 1, the upper bound is exclusive at (1+1,0,0)=(2,0,0)(1+1, 0, 0) = (2, 0, 0).
  • Evaluate each available version against the range [1.2.3,2.0.0)[1.2.3, 2.0.0):
    • 1.2.01.2.0 is rejected because 1.2.0<1.2.31.2.0 < 1.2.3.
    • 1.4.11.4.1 is accepted because 1.2.3≤1.4.1<2.0.01.2.3 \le 1.4.1 < 2.0.0.
    • 2.0.02.0.0 is rejected because 2.0.0≮2.0.02.0.0 \not< 2.0.0.
  • Identify the highest version among the remaining candidates. The only valid candidate is 1.4.11.4.1.
  • The final output is 1.4.1

Constraints

  • ^X.Y.Z means >= X.Y.Z and < (X+1).0.0 (assume X >= 1).
  • Compare versions as integer triples.
  • Return the highest match, else None.

Learn this in

More from ML Systems & Infrastructure 1

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.