runtime
299 / 500
question 299 Packaging & Databases · hard

Choose the newest allowed versions

A small installer must decide which version of each package to install. Write choose_versions(requirements, available): for every package named in the requirements, pick the newest version on the index that satisfies all of that package's requirements. A package can be required more than once (for example numpy>=1.26 from the project and numpy<2 from a plugin): both must hold.

Functionchoose_versions(requirements, available)
requirementsstrings such as "Requests>=2.31", "pandas>=2.2,<3", "rich"; operators >=, <=, ==, !=, >, <
availablea dictionary from package name to the list of versions on the index, in no particular order
Returnsa dictionary from the normalised name (lower case, runs of -_. as -) to the chosen version text, or None when no version fits or the package isn't on the index
Versionscompare as numbers part by part, missing parts counting as zero
Examples
callchoose_versions(['requests>=2.31', 'numpy>=1.26', 'numpy<2'], {'requests': ['2.30.0', '2.31.0', '2.32.3'], 'numpy': ['1.26.4', '2.1.0', '1.25.2']})returns{'requests': '2.32.3', 'numpy': '1.26.4'}
callchoose_versions(['pandas>=2.2,<3'], {'pandas': ['2.2.2', '3.0.1', '2.1.4', '2.10.0']})returns{'pandas': '2.10.0'}
callchoose_versions(['rich'], {'rich': ['13.7.1', '13.10.0', '12.6.0']})returns{'rich': '13.10.0'}

Loading your progress…

Choose the newest allowed versions — Python practice · Runtime