Skip to content

Instantly share code, notes, and snippets.

@MarcoF1
Created April 27, 2024 21:17
Show Gist options
  • Save MarcoF1/d35645adce675beb263b91729f9f119b to your computer and use it in GitHub Desktop.
Save MarcoF1/d35645adce675beb263b91729f9f119b to your computer and use it in GitHub Desktop.
from dataclasses import dataclass
from typing import Dict, List
@dataclass
class Item:
name: str
price: float
quantity: int
shared_by: List[str] # List of names of people sharing the item
TAX = 37.44
TIP = 84.40
items = [
# drinks
Item(name="Mojito", price=52.00, quantity=4, shared_by=["Marco", "Jordan","Jordan", "Chris"]), # not the unit cost but all 4 cost 52
Item(name="Daiquirina", price=15.00, quantity=1, shared_by=["Ricky"]),
Item(name="Caipirinha", price=15.00, quantity=1, shared_by=["Marvin"]),
Item(name="Red Sangria", price=13.00, quantity=1, shared_by=["Memo"]),
Item(name="Hemingway Daiquiri", price=15.00, quantity=1, shared_by=["Rila"]),
#
Item(name="Bacardi 10", price=64.00, quantity=5, shared_by=["Marco", "Chris", "Marvin", "Ricky"]),
Item(name="Bacardi 10 NEAT", price=16.00, quantity=1, shared_by=["Memo"]),
# food
Item(name="Bistec De Palomilla", price=28.00, quantity=1, shared_by=["Luis"]),
Item(name="Paella Vegetariana", price=23.00, quantity=1, shared_by=["Rila"]),
Item(name="Pollol Guajira", price=24.00, quantity=1, shared_by=["Arjun"]),
Item(name="Ropa Vieja", price=75.00, quantity=3, shared_by=["Marvin", "Ricky", "Jordan"]),
Item(name="Lechon Asado", price=25.00, quantity=1, shared_by=["Chris"]),
Item(name="Churrasco", price=30.00, quantity=1, shared_by=["Marco"]),
Item(name="Masitas de Puerco", price=27.00, quantity=1, shared_by=["Memo"]),
]
def calculate_total(items):
return sum([item.price for item in items])
print(f"Total (before tax + tip): {calculate_total(items)}\n")
def calculate_individual_totals(items, tax=TAX, tip=TIP):
totals = {}
total_cost = sum(item.price for item in items) + tax + tip
for item in items:
split_cost = (item.price / len(item.shared_by))
for person in item.shared_by:
if person in totals:
totals[person] += split_cost
else:
totals[person] = split_cost
# Distribute tax and tip
num_people = len(set(person for item in items for person in item.shared_by))
tax_per_person = tax / num_people
tip_per_person = tip / num_people
for person in totals:
totals[person] += tax_per_person + tip_per_person
return totals
totals = calculate_individual_totals(items)
memo_total = totals["Memo"]
print(f"Memo divided by 8: ${memo_total/8:.2f}\n")
total_cost = 0
for person, total in totals.items():
total_cost += total
if person == "Memo":
pass
else:
print(f"{person} owes: ${total + memo_total/8:.2f}")
print(f"\nTotal cost: ${total_cost:.2f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment