The Owner's Report

Data with PythonHard+10 XP+10

The Owner's Report

A bakery keeps one table of orders per month. The orders only store a product number. The prices are in a separate product list. The owner wants one report: how much money each city made in each month, plus a total.

Task: write revenue_report(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.
  • Return a list of rows, one per city that appears in any month. Each row is [city, revenue in month 1, revenue in month 2, ..., total], with the months in the same order as the keys of months.
  • A city with no orders in a month has 0.0 for that month.
  • Every money value is a float rounded to 2 decimal places.
  • Sort the rows by total, biggest first. If two cities have the same total, put them in alphabetical order.

Examples

months = {'Jan': {'city': ['Leeds', 'York', 'Leeds', 'Bath'], 'product_id': [1, 2, 2, 1], 'qty': [3, 2, 1, 4]}, 'Feb': {'city': ['York', 'Leeds', 'Bath', 'York'], 'product_id': [1, 1, 2, 3], 'qty': [5, 2, 1, 3]}}
products = {'product_id': [1, 2, 3], 'name': ['tea', 'cake', 'scone'], 'price': [2.5, 3.0, 2.0]}
returns [['York', 6.0, 18.5, 24.5], ['Leeds', 10.5, 5.0, 15.5], ['Bath', 10.0, 3.0, 13.0]]
months = {'Mar': {'city': ['Hull', 'Ely'], 'product_id': [7, 7], 'qty': [2, 1]}, 'Apr': {'city': ['Ely'], 'product_id': [8], 'qty': [3]}, 'May': {'city': ['Hull', 'Hull'], 'product_id': [8, 7], 'qty': [1, 1]}}
products = {'product_id': [7, 8], 'name': ['bun', 'pie'], 'price': [1.2, 4.0]}
returns [['Ely', 1.2, 12.0, 0.0, 13.2], ['Hull', 2.4, 0.0, 5.2, 7.6]]

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