Predict the output
Which versions are supported?
def parse(v: str) -> tuple[int, ...]:
return tuple(int(p) for p in v.split("."))
MIN_SUPPORTED = parse("4.10.0")
for v in ["4.9.3", "4.10.0", "4.12.1"]:
print(v, parse(v) >= MIN_SUPPORTED)Hint 1 of 1
Compare (4, 9, 3) with (4, 10, 0).
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 4.9.3 False, 4.10.0 True, 4.12.1 True
Correct. Tuples compare numerically, part by part.
Splitting on dots and converting to ints makes each version a tuple, and tuples compare item by item. 9 is less than 10 in the second position, so 4.9.3 is below the minimum, while 4.10.0 equals it and 4.12.1 is above.
Why not the others?
- B
4.9.3 True,4.10.0 True,4.12.1 True. That's what comparing the raw strings gives, since '9' > '1'. As tuples, (4, 9, 3) is below (4, 10, 0). - C
4.9.3 True,4.10.0 True,4.12.1 False. No comparison rule gives this: 4.12.1 is above the minimum both as strings and as numbers. - D
4.9.3 False,4.10.0 False,4.12.1 True. Equal counts as supported (>=).