Skip to content

Commit 03ac4e2

Browse files
Merge pull request #159 from alexanderthclark/codex/implement-__repr__-and-_repr_latex_-methods
Add repr methods for Monopoly
2 parents cdfab7a + 5c403ad commit 03ac4e2

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

freeride/monopoly.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,11 @@ def _solve(self):
103103
self.q = best_q
104104
self.p = self.demand.p(best_q)
105105
self.profit = best_profit
106+
107+
def __repr__(self) -> str:
108+
"""Return a concise text summary of the monopoly outcome."""
109+
return f"Monopoly: Q = {self.q:g}, P = {self.p:g}, Profit = {self.profit:g}"
110+
111+
def _repr_latex_(self) -> str:
112+
"""Return a LaTeX summary for notebook display."""
113+
return f"$Q^* = {self.q:g},\\ P^* = {self.p:g},\\ \\Pi = {self.profit:g}$"

tests/test_monopoly.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ def test_basic_outcome(self):
1414
self.assertAlmostEqual(m.p, 6.0)
1515
self.assertAlmostEqual(m.profit, 16.0)
1616

17+
def test_repr_methods(self):
18+
"""Monopoly representations should summarize the solved outcome."""
19+
monopoly = Monopoly(Demand(10, -1), Cost(0, 2))
20+
self.assertEqual(repr(monopoly), "Monopoly: Q = 4, P = 6, Profit = 16")
21+
self.assertEqual(
22+
monopoly._repr_latex_(),
23+
"$Q^* = 4,\\ P^* = 6,\\ \\Pi = 16$",
24+
)
25+
1726
def test_piecewise_demand_zero_cost(self):
1827
"""Profit maximization with piecewise demand and zero cost."""
1928
demand = Demand([10, 5], [-1, -1])

0 commit comments

Comments
 (0)