Each City's Best Seller

Data with PythonHard+10 XP+10

Each City's Best Seller

The bakery wants to put its most profitable product in each shop window. The orders are split across months, store only a product number, and the names and prices are in a separate product list.

Task: write best_sellers(months, products).

  • months is a dictionary from a month's name to that month's orders. Each month's orders are a dictionary of three lists: "city", "product_id" and "qty". Every month has at least one order.
  • products is a dictionary of three lists: "product_id", "name" and "price". Every product_id used in an order is in it, once.
  • An order's revenue is its qty times its product's price.
  • For each city, add up the revenue of each product name across all months.
  • Return a dictionary from each city to the name of the product that earned that city the most.
  • If two products tie for a city, pick the name that comes first alphabetically. A tie means the two totals are equal once rounded to 2 decimal places.

Examples

months = {'Jan': {'city': ['Leeds', 'York', 'Leeds', 'Bath'], 'product_id': [1, 2, 2, 3], 'qty': [1, 4, 3, 2]}, 'Feb': {'city': ['York', 'Leeds', 'Bath'], 'product_id': [1, 3, 3], 'qty': [2, 2, 1]}}
products = {'product_id': [1, 2, 3], 'name': ['tea', 'cake', 'scone'], 'price': [2.5, 3.0, 2.0]}
returns {'Bath': 'scone', 'Leeds': 'cake', 'York': 'cake'}
months = {'Mar': {'city': ['Hull', 'Hull', 'Ely'], 'product_id': [1, 2, 2], 'qty': [6, 1, 1]}, 'Apr': {'city': ['Hull', 'Ely'], 'product_id': [2, 1], 'qty': [1, 1]}}
products = {'product_id': [1, 2], 'name': ['bun', 'pie'], 'price': [1.0, 3.0]}
returns {'Ely': 'pie', 'Hull': 'bun'}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run