Highest Semver Matching a Caret Range
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
print(resolve_caret(["1.2.0", "1.4.1", "2.0.0"], "^1.2.3"))
1.4.1
- Parse the constraint
^1.2.3to establish the valid version range. The lower bound is inclusive at , and since the major version is , the upper bound is exclusive at . - Evaluate each available version against the range :
- is rejected because .
- is accepted because .
- is rejected because .
- Identify the highest version among the remaining candidates. The only valid candidate is .
- The final output is 1.4.1
Constraints
^X.Y.Zmeans>= X.Y.Zand< (X+1).0.0(assumeX >= 1).- Compare versions as integer triples.
- Return the highest match, else None.