#C274. Expense Category Aggregator
Expense Category Aggregator
Expense Category Aggregator
You are given a list of expense logs recorded with an amount (a floating point number), a category (a string), and a date (in YYYY-MM-DD format). Your task is to aggregate the total expense for each category.
For a given category c, the total expense is computed as
where \(S_c\) is the set of all expense logs with category c and \(a_i\) is the expense amount for the log \(i\).
You should output the aggregated totals in JSON format with categories as keys and their corresponding total amounts as values.
inputFormat
The input is provided via standard input (stdin) in the following format:
- The first line contains an integer n (0 ≤ n ≤ 105) representing the number of expense logs.
- The following n lines each contain an expense log with three fields separated by spaces:
- amount: a floating-point number representing the expense amount
- category: a string (without spaces) representing the expense category
- date: a date in the format YYYY-MM-DD (this field is not used in the aggregation)
outputFormat
Output via standard output (stdout) a JSON object (in one line) representing the aggregated expenses by category. The keys are categories, and the values are the corresponding total amounts (as floating point numbers).
For example, an output could be:
{"entertainment": 5.0, "food": 35.5, "utilities": 100.0}## sample
4
20.5 food 2023-01-01
15.0 food 2023-01-02
5.0 entertainment 2023-01-03
100.0 utilities 2023-01-04
{"entertainment": 5.0, "food": 35.5, "utilities": 100.0}