#242 · Working with data · Beginner · 35 XP · about 7 min
A shop keeps its stock as a list of dictionaries such as {'item': 'pens', 'stock': 4, 'reorder_level': 10}. An item needs restocking when its stock is below its reorder level, and its shortfall is reorder_level - stock.
Write restock_list(inventory) that returns a list of (item, shortfall) tuples for every item that needs restocking, the largest shortfall first. Items with the same shortfall go in alphabetical order. An item whose stock equals its reorder level does not need restocking yet.
inventory = [ {'item': 'pens', 'stock': 4, 'reorder_level': 10}, {'item': 'paper', 'stock': 50, 'reorder_level': 20}, {'item': 'glue', 'stock': 0, 'reorder_level': 5}, {'item': 'tape', 'stock': 3, 'reorder_level': 3}, {'item': 'clips', 'stock': 2, 'reorder_level': 8}, ] restock_list(inventory) # returns [('clips', 6), ('pens', 6), ('glue', 5)]
Clips and pens are both 6 short, so they go in alphabetical order. Tape is exactly at its reorder level, so it is not on the list.
0 <= len(inventory) <= 1000First solves only, ranked by stars and then time.