#K13121. Monthly Spending Calculation
Monthly Spending Calculation
Monthly Spending Calculation
You are given a ledger of expenses where each entry is represented as a line containing a date, a category, and an expenditure amount. The date is in the format YYYY-MM-DD
, the category is one of the following: Food, Transport, Entertainment, and Utilities, and the amount is a positive integer.
Your task is to calculate the total amount spent in each category for a specific month. The month is provided in the format YYYY-MM
and an entry is considered for the calculation if its date starts with the given month.
The totals for the four categories should be output in the order: Food, Transport, Entertainment, Utilities. Mathematically, if \( A_i \) is the amount in an entry corresponding to category \( C \) and the date of that entry satisfies \( \text{date}[0:7] = \text{query_month} \), then the total spending in category \( C \) is given by:
[ S_C = \sum_{\substack{\text{entry } i \ \text{if } date_i[0:7]=\text{query_month} \text{ and } category_i=C}} A_i ]
If there are no entries for a category in the specified month, output 0 for that category.
inputFormat
The input is read from standard input (stdin) and has the following format:
n entry_1 entry_2 ... entry_n query_month
Here, n
is a positive integer representing the number of ledger entries. Each entry
is a string with three parts separated by spaces: the date (YYYY-MM-DD
), the category (one of Food, Transport, Entertainment, Utilities), and the amount (a positive integer). The final line is a string query_month
in the format YYYY-MM
representing the month for which you need to calculate the spending totals.
outputFormat
The output is printed on standard output (stdout) as a single line containing four integers separated by a space. These integers represent the total spending in the categories Food, Transport, Entertainment, and Utilities respectively for the given month.
## sample5
2023-08-15 Food 20
2023-08-16 Transport 15
2023-08-15 Entertainment 30
2023-08-16 Food 25
2023-09-01 Utilities 50
2023-08
45 15 30 0