#C13128. Monthly Expense Summarization
Monthly Expense Summarization
Monthly Expense Summarization
Given a list of financial transactions, each containing a date, an expense amount, and a category, write a program that aggregates the total expenses for each category on a monthly basis. Each transaction is represented by a triple:
(Date, Amount, Category)
The date is provided in the format YYYY-MM-DD, and the month should be extracted as YYYY-MM. For every month, compute the total expense for each category. The aggregation can be expressed as: $$Total = \sum_{i=1}^{n}\, expense_i.$$
The input will be read from standard input (stdin) and the output should be printed to standard output (stdout) in a JSON dictionary format. In the output dictionary, each key is a month in the format YYYY-MM
and its corresponding value is a list of lists. Each inner list consists of a category and the total amount spent in that category during that month. The months should appear in ascending order based on their first occurrence and the categories for each month should be listed in the order they appear in the input.
inputFormat
The input is read from standard input (stdin) and is structured as follows:
- The first line contains an integer
n
, representing the number of transactions. - Each of the following
n
lines contains a single transaction in the format:
Date Amount Category
where:
Date
is in the formatYYYY-MM-DD
.Amount
is a floating point number.Category
is a string without spaces.
outputFormat
The output should be printed to standard output (stdout) as a JSON dictionary. Each key is a month in the format YYYY-MM
and the corresponding value is a list of lists. Each inner list contains two elements: the category name (a string) and the total expense (a floating point number) for that category in that month. The order of the months should be the same as their first occurrence in the input, and within each month, the categories should appear in the order they appear in the input.
6
2023-01-15 50.00 Groceries
2023-01-20 200.00 Electronics
2023-02-10 30.00 Groceries
2023-02-15 20.00 Transportation
2023-01-30 80.00 Utilities
2023-02-28 150.00 Electronics
{"2023-01": [["Groceries", 50.0], ["Electronics", 200.0], ["Utilities", 80.0]], "2023-02": [["Groceries", 30.0], ["Transportation", 20.0], ["Electronics", 150.0]]}