Expense Tacking and Categorization - codepath/compsci_guides GitHub Wiki

Unit 4 Session 2 Standard (Click for link to problem statements)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What is the goal of the problem?
    • A: The goal is to calculate the total expenses for each category and identify the category with the highest total expense.
  • Q: What are the inputs?
    • A: The input is a list of tuples, where each tuple contains a string representing an expense category and a float representing the expense amount.
  • Q: What are the outputs?
    • A: The output is a dictionary with categories as keys and their total expenses as values, and a string representing the category with the highest total expense.
  • Q: Can there be ties for the highest expense?
    • A: The problem assumes there is a unique category with the highest expense.
  • Q: How should expenses be handled if a category appears multiple times?
    • A: Expenses should be summed across all entries for each category.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use a dictionary to accumulate expenses for each category. As we accumulate, track the category with the highest total expense. Return the dictionary of accumulated expenses and the category with the highest total.

1) Initialize an empty dictionary `expense_dict` to store the total expenses for each category.
2) Initialize variables `max_category` to track the category with the highest expense and `max_expense` to track the highest expense amount.
3) Iterate through each `(category, amount)` tuple in the `expenses` list:
   a) If `category` is already in `expense_dict`, add `amount` to `expense_dict[category]`.
   b) If `category` is not in `expense_dict`, set `expense_dict[category]` to `amount`.
   c) If the updated total for `category` is greater than `max_expense`, update `max_category` and `max_expense`.
4) Return the `expense_dict` and `max_category`.

**⚠️ Common Mistakes**

- Forgetting to initialize the category in the dictionary before adding amounts.
- Not updating `max_category` and `max_expense` correctly as expenses are summed.
- Assuming categories are unique when they may not be.

I-mplement

def calculate_expenses(expenses):
    expense_dict = {}
    max_category = None
    max_expense = float('-inf')

    for category, amount in expenses:
        if category in expense_dict:
            expense_dict[category] += amount
        else:
            expense_dict[category] = amount

        # Update max_category if this category now has the highest expense
        if expense_dict[category] > max_expense:
            max_expense = expense_dict[category]
            max_category = category

    return expense_dict, max_category
Example Usage:

expenses = [("Food", 12.5), ("Transport", 15.0), ("Accommodation", 50.0),
            ("Food", 7.5), ("Transport", 10.0), ("Food", 10.0)]
print(calculate_expenses(expenses))  # Output: ({'Food': 30.0, 'Transport': 25.0, 'Accommodation': 50.0}, 'Accommodation')

expenses_2 = [("Entertainment", 20.0), ("Food", 15.0), ("Transport", 10.0),
              ("Entertainment", 5.0), ("Food", 25.0), ("Accommodation", 40.0)]
print(calculate_expenses(expenses_2))  # Output: ({'Entertainment': 25.0, 'Food': 40.0, 'Transport': 10.0, 'Accommodation': 40.0}, 'Food')

expenses_3 = [("Utilities", 100.0), ("Food", 50.0), ("Transport", 75.0),
              ("Utilities", 50.0), ("Food", 25.0)]
print(calculate_expenses(expenses_3))  # Output: ({'Utilities': 150.0, 'Food': 75.0, 'Transport': 75.0}, 'Utilities')