Suspicious Receipts

Data with PythonMedium+10 XP+10

Suspicious Receipts

An office checks its expense receipts. Any receipt whose amount is an outlier by the 1.5 × IQR rule gets a second look, and the finance team also wants the average of the ordinary receipts.

Task: write check_receipts(receipts).

  • receipts is a list of dictionaries, each with receipt (an id) and amount.
  • Work out the fences from the amount column, with Q1 and Q3 from pandas quantile(0.25) and quantile(0.75).
  • A receipt is flagged if its amount is strictly below the low fence or strictly above the high fence.

Return a dictionary with two keys:

  • "flagged": the ids of the flagged receipts, in the original order ([] if none)
  • "typical_mean": the mean amount of the receipts that were not flagged, rounded to 2 decimal places

There are always at least two receipts.

Examples

receipts = [{"receipt": "R1", "amount": 12.4}, {"receipt": "R2", "amount": 15.1}, {"receipt": "R3", "amount": 240.0}, {"receipt": "R4", "amount": 9.8}, {"receipt": "R5", "amount": 14.6}, {"receipt": "R6", "amount": 11.2}]
returns {'flagged': ['R3'], 'typical_mean': 12.62}
receipts = [{"receipt": "A", "amount": 20.0}, {"receipt": "B", "amount": 22.0}, {"receipt": "C", "amount": 25.0}]
returns {'flagged': [], 'typical_mean': 22.33}

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