One Trend From Many Files

Data with PythonMedium+10 XP+10

One Trend From Many Files

A farm shop's till sends a new file every few days. Each file lists sales records: the day number and the amount of one sale. A day can have many sales, and because the files overlap, one day's sales can be spread across several files.

The owner wants the trend: the total sales of each day, smoothed with a rolling average.

Task: write daily_trend(files, window).

  • files is a list of tables. Each table is a dictionary of two lists, "day" (whole numbers) and "sales". Every table has at least one row.
  • First add up all the sales for each day, across every file. Every day number from the smallest to the largest has at least one sale somewhere.
  • Then take a rolling average of those daily totals over window days, in day order.
  • Return a list of [day, average] pairs, one for each day where the window is full, in day order. day is the last day in the window, as an int. average is a float rounded to 1 decimal place.
  • If there are fewer days than window, return an empty list.

Examples

files = [{"day": [1, 2, 2, 3], "sales": [10, 5, 7, 9]}, {"day": [3, 4, 5], "sales": [3, 15, 6]}]
window = 2
returns [[2, 11.0], [3, 12.0], [4, 13.5], [5, 10.5]]
files = [{"day": [1, 2, 3], "sales": [30, 12, 18]}]
window = 3
returns [[3, 20.0]]

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