Merge the Weekly Counts

Python for AIHard+10 XP+10

Merge the Weekly Counts

Every week, a script saves how often certain words appeared in your notes, as a JSON file holding one dictionary:

json
1{"data": 3, "model": 1}

Write merge_counts(paths, out_path, min_count) that:

  1. reads every JSON file listed in paths, in order
  2. adds the counts together, word by word (a word missing from a file counts as 0 there)
  3. keeps only words whose total is at least min_count
  4. saves the result to out_path with json.dump
  5. reads out_path back and returns what it read

If paths is empty, or no word reaches min_count, save and return an empty dictionary. The starter code creates week1.json to week4.json. Keep those lines.

Examples

paths = ["week1.json", "week2.json"]
out_path = "total.json"
min_count = 1
returns {'data': 3, 'model': 5, 'python': 2}
paths = ["week1.json", "week2.json", "week3.json"]
out_path = "busy.json"
min_count = 4
returns {'data': 4, 'model': 5}

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