-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (112 loc) · 4.05 KB
/
main.py
File metadata and controls
132 lines (112 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
"""
Double Entry Ledger Demo - Water Tank System
Demonstrates how double-entry principles can track variable changes clearly
"""
class DoubleEntryLedger:
def __init__(self):
self.transactions = []
self.balances = {}
def record_transfer(self, description, from_account, to_account, amount):
"""Record a transfer from one account to another"""
# Record the transaction
transaction = {
'description': description,
'from': from_account,
'to': to_account,
'amount': amount
}
self.transactions.append(transaction)
# Update balances: decrease source, increase destination
self.balances[from_account] = self.balances.get(from_account, 0) - amount
self.balances[to_account] = self.balances.get(to_account, 0) + amount
print(f"✓ {description}")
print(f" {from_account}: -{amount} (now {self.balances[from_account]})")
print(f" {to_account}: +{amount} (now {self.balances[to_account]})")
print()
def get_balance(self, account):
return self.balances.get(account, 0)
def print_balances(self):
print("Current Balances:")
for account, balance in self.balances.items():
print(f" {account}: {balance} liters")
print()
def verify_conservation(self):
"""Verify that total water is conserved across all accounts"""
total = sum(self.balances.values())
print(f"Total water in system: {total} liters")
return total
# Demo: Water Tank System
ledger = DoubleEntryLedger()
print("=== Water Tank System Demo ===")
print("Tracking water transfers - every drop must come from somewhere and go somewhere")
print()
def show_status():
ledger.verify_conservation()
ledger.print_balances()
# Start with water in an external reservoir
ledger.record_transfer(
"Fill Tank A from reservoir",
from_account="External_Reservoir",
to_account="Tank_A",
amount=100
)
show_status()
# Transfer water between tanks
ledger.record_transfer(
"Transfer from Tank A to Tank B",
from_account="Tank_A",
to_account="Tank_B",
amount=30
)
show_status()
# Add more water to Tank A
ledger.record_transfer(
"Pump water into Tank A",
from_account="Water_Pump_Source",
to_account="Tank_A",
amount=20
)
show_status()
# Tank B springs a leak
ledger.record_transfer(
"Tank B leaks to ground",
from_account="Tank_B",
to_account="Environment",
amount=5
)
show_status()
# Show results
ledger.print_balances()
def print_dynamic_verification(ledger):
print("=== Verification ===")
# Collect all accounts from transactions
accounts = set()
for t in ledger.transactions:
accounts.add(t['from'])
accounts.add(t['to'])
for account in sorted(accounts):
gained = sum(t['amount'] for t in ledger.transactions if t['to'] == account)
lost = sum(t['amount'] for t in ledger.transactions if t['from'] == account)
expected = 0 + gained - lost
# Collect individual transactions for this account
txns = []
for t in ledger.transactions:
if t['to'] == account:
txns.append(f"+{t['amount']}")
if t['from'] == account:
txns.append(f"-{t['amount']}")
txns_str = ', '.join(txns)
print(f"{account}: started with 0, gained {gained}, lost {lost} ({txns_str}) = {expected} liters")
print(f"{account} actual: {ledger.get_balance(account)} liters\n")
print_dynamic_verification(ledger)
# The key insight: water is conserved!
print("=== Conservation Check ===")
total = ledger.verify_conservation()
print("Notice: Total water = 0 because we track sources as negative")
print("This makes sense: we 'borrowed' 120L from external sources,")
print("and now have 90+25+5 = 120L in our system")
print()
print("=== Transaction History ===")
for i, t in enumerate(ledger.transactions, 1):
print(f"{i}. {t['description']}: {t['amount']}L from {t['from']} to {t['to']}")