Created
January 14, 2026 14:19
-
-
Save jacobsapps/7c8796ddb4e113c0f2fa18098f137258 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct PurchaseEvent: Hashable { | |
| let customerId: String | |
| let date: String | |
| let amount: Double | |
| } | |
| final class RevenueTracker { | |
| private var totalsByDate: [String: [String: Double]] = [:] | |
| func addPurchase(_ event: PurchaseEvent) { | |
| var totals = totalsByDate[event.date, default: [:]] | |
| totals[event.customerId, default: 0] += event.amount | |
| totalsByDate[event.date] = totals | |
| } | |
| func averageRevenuePerCustomer(on date: String) -> Double { | |
| guard let totals = totalsByDate[date], !totals.isEmpty else { | |
| return 0 | |
| } | |
| let totalRevenue = totals.values.reduce(0, +) | |
| return totalRevenue / Double(totals.count) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment