Editor

Can We Fill the Order?

#562 · Dictionaries and sets · Beginner · Find and fix · 35 XP · about 5 min

A small shop keeps its stock in a dictionary that maps each product to how many are on the shelf, such as {'apple': 5, 'pear': 2}. An order is a dictionary in the same shape. can_fill(stock, order) should return True when the shop has at least as many of every ordered product as the order asks for, and False otherwise. An empty order can always be filled.

The code in the editor has two bugs. An order for exactly what is left on the shelf is refused, and an order for something the shop has never stocked crashes with a KeyError.

Examples

stock = {'apple': 5, 'pear': 2}
can_fill(stock, {'apple': 3})  # returns True
can_fill(stock, {'apple': 5})  # returns True
can_fill(stock, {'pear': 3})   # returns False
can_fill(stock, {'kiwi': 1})   # returns False

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.