Matching Versions Against Ranges

medium
Unit 22 · Bisect0/0 to milestone

Package managers accept version ranges. Implement satisfies(version, spec) for versions MAJOR.MINOR.PATCH (integers) and specs made of space-separated comparators, all of which must hold:

  • >=1.2.3, >1.2.3, <=1.2.3, <1.2.3, =1.2.3 (or a bare 1.2.3)
  • caret ^1.2.3: >=1.2.3 <2.0.0; for 0.x, caret means >=0.2.3 <0.3.0 (if MAJOR is 0, the MINOR can't change)
  • tilde ~1.2.3: >=1.2.3 <1.3.0
  • || separates alternatives (any may match)
Pro problem

Unlock this and every premium problem across all tracks — plus hidden-test grading, advanced AI engineering modules and new content every week.

Start 7-day free trial
solution.py · Python 3.13
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Matching Versions Against Ranges · TechGradify