#K90742. Budget Management: Expenses Categorization and Budget Checking
Budget Management: Expenses Categorization and Budget Checking
Budget Management: Expenses Categorization and Budget Checking
You are given a budget and a list of expenses. Each expense consists of a category (a string) and an amount (a float). Your task is two-fold:
- Create a dictionary (or map) that categorizes the expenses by summing the amounts for each category.
- Check the overall spending against the given budget and generate a message. If the total expenses do not exceed the budget, output the remaining budget; otherwise, output by how much the expenses exceed the budget.
Both results must be output to standard output. The first output line should display the dictionary in JSON format with keys sorted in lexicographical order and each value formatted to two decimal places. The second output line should display the budget check message in the form:
\(\texttt{Remaining budget: X dollars}\) when under budget or \(\texttt{Over budget by X dollars}\) when over budget, with \(X\) formatted to exactly two decimal places.
inputFormat
The input is given from standard input in the following format:
- The first line contains a float number representing the total budget.
- The second line contains an integer (n), the number of expenses.
- Each of the next (n) lines contains a category (string) and an amount (float) separated by space.
outputFormat
The output should consist of two lines written to standard output:
- The first line is a JSON formatted dictionary where each key is a category and its value is the total expense for that category, printed with two decimal places and keys sorted in lexicographical order.
- The second line is a message that is either:
- "Remaining budget: X dollars" if the total expenses are less than or equal to the budget, or
- "Over budget by X dollars" if the expenses exceed the budget,
with (X) formatted to exactly two decimal places.## sample
1000
4
food 250.45
entertainment 100.50
transport 120.00
food 120.55
{"entertainment": 100.50, "food": 371.00, "transport": 120.00}
Remaining budget: 408.50 dollars
</p>