Hello devs,
I need help with my current project. I am building a Python application that tracks expenses for a group of users (Split Wise analog).
You can create “groups” and add users to these groups. A group can have one or more expenses (each expense is a number in USD). The application tracks how much each user paid for each expense.
The application should be able to tell the balance: how much any user owes to any other user based on group expenses (assuming all users should spend an equal amount for summary expenses). I need an algorithm how to calculate the balance.
Let’s take a look at a simplified example.
Say, we have a “Party” group with 3 users, John, Mary, and Maya. There are 2 expenses related to the “Party” group: “wine” ($30) and “food” ($60). Users paid:
Expense: “Wine” (30):
- John: 0
- Mary: 0
- Maya: 30
Expense: “Food” (60):
- John: 60
- Mary: 0
- Maya: 0
This means Maya bought all wine for $30 and John bought all food for $60.
let’s calculate the balance for the “Party” group.
Wine expense balance:
- John owes Mary 0
- John owes Maya 10
- Mary owes John 0
- Mary owes Maya 10
- Maya owes John -10
- Maya owes Mary -10
Hence, each user will spend 10.
Food Expense balance:
- John owes Mary -20
- John owes Maya -20
- Mary owes John 20
- Mary owes Maya 10
- Maya owes John 20
- Maya owes Mary 0
Hence, each user will spend 20.
Total balance for the “Party” group:
- John owes Mary -20
- John owes Maya -10
- Mary owes John 20
- Mary owes Maya 10
- Maya owes John 10
- Maya owes Mary -10
So the total spend of the group = 60 + 30 = 90. Each user should spend 90/3 = 30
John, Mary, and Maya will spend $30 each after:
Mary pays John 20 and Maya 10,
Maya pays John 10.
I need an algorithm (ideally a Python code) how to calculate the user balance for the group, sort of:
def calculate_balance(group_expenses):
balance = {}
# Do the magic here
return balance
# Test the function with the example data
group_expenses = [
{"John": 0, "Mary": 0, "Maya": 30},
{"John": 60, "Mary": 0, "Maya": 0}
]
balance = calculate_balance(group_expenses)
print("John's balance:", balance["John"]) # {Mary: -20, Maya: -10}
print("Mary's balance:", balance["Mary"]) # {John:20, Maya: 10}
print("Maya's balance:", balance["Maya"]) # {John: 10, Mary: -10}
Because the example data is super simple and even I was able to sort out results intuitively. But at this point, I can’t develop a formula for calculating balance mathematically.