#C303. Expense Summary by Category
Expense Summary by Category
Expense Summary by Category
You are given a list of financial transactions and a target month. Each transaction contains a date, an amount, and a category. Your task is to summarize the total expense for each category for transactions that occurred in the given month. A transaction is considered part of the month if its date starts with the specified month (in the format YYYY-MM).
Note: The amount can be negative, representing a refund or correction. The results should be printed in alphabetical order of category names. If no transaction falls within the specified month, output nothing.
Formula: For each category \(c\), the total expense is computed as:
[ Total(c) = \sum_{\substack{transaction \in Transactions \ transaction.date ;\text{starts with}; month \ transaction.category = c}} transaction.amount ]
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer
n
representing the number of transactions. - The next
n
lines each describe a transaction with three fields separated by spaces:date
: A string in the formatYYYY-MM-DD
.amount
: A floating-point number representing the transaction amount.category
: A string (without spaces) indicating the transaction category.
- The final line contains a string representing the month in the format
YYYY-MM
for which the expenses should be summarized.
outputFormat
Output to standard output (stdout) the summary of total expenses for each category in the specified month. For each applicable category, print a line containing the category name and the total expense (as a floating-point number) separated by a space. The output should be sorted in alphabetical order of the category names. If no transactions match the given month, output nothing.
## sample2
2023-07-01 50.0 Groceries
2023-07-05 20.0 Groceries
2023-07
Groceries 70.0