Editor

Corner Shop Inventory

#629 · Working with data · Intermediate · Mini-project · 160 XP · about 60 min

Build the stock system of a corner shop. The stock is a dictionary that maps each item to how many the shop has, for example {'milk': 4, 'eggs': 30}. Prices are in a second dictionary, in cents: {'milk': 129}.

Parts

  • Part 1 receive(stock, item, qty): a delivery of qty of an item (a new item starts at 0). Return the item's new quantity.
  • Part 2 sell(stock, item, qty): if the shop has at least qty of the item, take them off and return True. Otherwise change nothing and return False. An item the shop has never stocked cannot be sold.
  • Part 3 run_day(stock, events): events is a list of ('in', item, qty) deliveries and ('sale', item, qty) sales, in the order they happened. Apply each one with Part 1 or Part 2, and return a list of the sales that could not be made, as (item, qty) tuples in order.
  • Part 4 restock_alerts(stock, levels): levels maps an item to a pair (minimum, order_up_to). An item needs ordering when the shop has fewer than its minimum (an item missing from stock counts as 0), and the amount to order is order_up_to minus what the shop has. Return a list of (item, amount) tuples, the largest amount first, equal amounts in alphabetical order. Items with no level are not checked.
  • Part 5 stock_value(stock, prices): what everything in stock is worth, written as dollars and cents with a comma between thousands, like '$1,859.57'. An item with no price is worth nothing.

Examples

stock = {'milk': 2}
receive(stock, 'bread', 5)        # returns 5
sell(stock, 'milk', 3)            # returns False: only 2 left
run_day(stock, [('sale', 'milk', 1), ('sale', 'jam', 1)])
# returns [('jam', 1)], and stock is now {'milk': 1, 'bread': 5}

restock_alerts(stock, {'milk': (5, 20), 'bread': (2, 10)})
# returns [('milk', 19)]
stock_value(stock, {'milk': 129, 'bread': 250})   # returns '$13.79'

The shop has 1 milk, below its minimum of 5, so it orders 20 - 1 = 19. The stock is worth 1 x 129 + 5 x 250 = 1,379 cents.

Constraints

  • Quantities are whole numbers, qty is at least 1, and prices are whole numbers of cents.
  • A shop has at most 2,000 items.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.