Skip to content

Commit 1c7e698

Browse files
committed
challenge finished states
1 parent afae010 commit 1c7e698

File tree

4 files changed

+181
-8
lines changed

4 files changed

+181
-8
lines changed

Finished/Ch 1/challenge.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
# Programming challenge: define a class to represent a stock symbol
33

44

5-
class StockSymbol:
5+
class Stock:
66
def __init__(self, ticker, price, company) -> None:
77
self.ticker = ticker
88
self.price = price
99
self.company = company
1010

1111
def get_description(self):
12-
pass
13-
12+
return f"{self.ticker}: {self.company} -- ${self.price}"
13+
14+
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
15+
msft = Stock("MSFT", 342.0, "Microsoft Corp")
16+
goog = Stock("GOOG", 135.0, "Google Inc")
17+
meta = Stock("META", 275.0, "Meta Platforms Inc")
18+
amzn = Stock("AMZN", 135.0, "Amazon Inc")
19+
20+
print(msft.get_description())
21+
print(goog.get_description())
22+
print(meta.get_description())
23+
print(amzn.get_description())

Finished/Ch 2/challenge.py

+50-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,58 @@
11
# Python Object Oriented Programming by Joe Marini course example
22
# Programming challenge: use inheritance and abstract classes
33

4-
from abc import ABC
4+
from abc import ABC, abstractmethod
55

66
class Asset(ABC):
7-
pass
7+
def __init__(self, price):
8+
self.price = price
89

9-
class StockSymbol(Asset):
10-
pass
10+
@abstractmethod
11+
def get_description(self):
12+
pass
13+
14+
class Stock(Asset):
15+
def __init__(self, ticker, price, company):
16+
super().__init__(price)
17+
self.company = company
18+
self.ticker = ticker
19+
20+
def get_description(self):
21+
return f"{self.ticker}: {self.company} -- ${self.price}"
1122

1223
class Bond(Asset):
13-
pass
24+
def __init__(self, price, description, duration, yieldamt):
25+
super().__init__(price)
26+
self.description = description
27+
self.duration = duration
28+
self.yieldamt = yieldamt
29+
30+
def get_description(self):
31+
return f"{self.description}: {self.duration}yr : ${self.price} : {self.yieldamt}%"
32+
33+
34+
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
35+
try:
36+
ast = Asset(100.0)
37+
except:
38+
print("Can't instantiate Asset!")
39+
40+
msft = Stock("MSFT", 342.0, "Microsoft Corp")
41+
goog = Stock("GOOG", 135.0, "Google Inc")
42+
meta = Stock("META", 275.0, "Meta Platforms Inc")
43+
amzn = Stock("AMZN", 135.0, "Amazon Inc")
44+
45+
us30yr = Bond(95.31, "30 Year US Treasury", 30, 4.38)
46+
us10yr = Bond(96.70, "10 Year US Treasury", 10, 4.28)
47+
us5yr = Bond(98.65, "5 Year US Treasury", 5, 4.43)
48+
us2yr = Bond(99.57, "2 Year US Treasury", 2, 4.98)
49+
50+
print(msft.get_description())
51+
print(goog.get_description())
52+
print(meta.get_description())
53+
print(amzn.get_description())
54+
55+
print(us30yr.get_description())
56+
print(us10yr.get_description())
57+
print(us5yr.get_description())
58+
print(us2yr.get_description())

Finished/Ch 3/challenge.py

+62
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
11
# Python Object Oriented Programming by Joe Marini course example
22
# Programming challenge: add methods for comparison and equality
33

4+
from abc import ABC, abstractmethod
5+
6+
7+
class Asset(ABC):
8+
def __init__(self, price):
9+
self.price = price
10+
11+
@abstractmethod
12+
def __str__(self):
13+
pass
14+
15+
16+
class Stock(Asset):
17+
def __init__(self, ticker, price, company):
18+
super().__init__(price)
19+
self.company = company
20+
self.ticker = ticker
21+
22+
def __str__(self):
23+
return f"{self.ticker}: {self.company} -- ${self.price}"
24+
25+
def __lt__(self, other):
26+
return self.price < other.price
27+
28+
29+
class Bond(Asset):
30+
def __init__(self, price, description, duration, yieldamt):
31+
super().__init__(price)
32+
self.description = description
33+
self.duration = duration
34+
self.yieldamt = yieldamt
35+
36+
def __str__(self):
37+
return f"{self.description}: {self.duration}yr : ${self.price} : {self.yieldamt}%"
38+
39+
def __lt__(self, other):
40+
return self.yieldamt < other.yieldamt
41+
42+
43+
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
44+
stocks = [
45+
Stock("MSFT", 342.0, "Microsoft Corp"),
46+
Stock("GOOG", 135.0, "Google Inc"),
47+
Stock("META", 275.0, "Meta Platforms Inc"),
48+
Stock("AMZN", 120.0, "Amazon Inc")
49+
]
50+
51+
bonds = [
52+
Bond(95.31, "30 Year US Treasury", 30, 4.38),
53+
Bond(96.70, "10 Year US Treasury", 10, 4.28),
54+
Bond(98.65, "5 Year US Treasury", 5, 4.43),
55+
Bond(99.57, "2 Year US Treasury", 2, 4.98)
56+
]
57+
58+
stocks.sort()
59+
bonds.sort()
60+
61+
for stock in stocks:
62+
print(stock)
63+
print("-----------")
64+
for bond in bonds:
65+
print(bond)

Finished/Ch 4/challenge.py

+56
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,60 @@
22
# Programming challenge: implement a dataclass
33

44
from dataclasses import dataclass
5+
from abc import ABC, abstractmethod
56

7+
@dataclass
8+
class Asset(ABC):
9+
price: float
10+
11+
@abstractmethod
12+
def __lt__(self, other):
13+
pass
14+
15+
16+
@dataclass
17+
class Stock(Asset):
18+
ticker: str
19+
company: str
20+
21+
def __lt__(self, other):
22+
return self.price < other.price
23+
24+
@dataclass
25+
class Bond(Asset):
26+
description: str
27+
duration: int
28+
yieldamt: float
29+
30+
def __lt__(self, other):
31+
return self.yieldamt < other.yieldamt
32+
33+
34+
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
35+
stocks = [
36+
Stock("MSFT", 342.0, "Microsoft Corp"),
37+
Stock("GOOG", 135.0, "Google Inc"),
38+
Stock("META", 275.0, "Meta Platforms Inc"),
39+
Stock("AMZN", 120.0, "Amazon Inc")
40+
]
41+
42+
bonds = [
43+
Bond(95.31, "30 Year US Treasury", 30, 4.38),
44+
Bond(96.70, "10 Year US Treasury", 10, 4.28),
45+
Bond(98.65, "5 Year US Treasury", 5, 4.43),
46+
Bond(99.57, "2 Year US Treasury", 2, 4.98)
47+
]
48+
49+
try:
50+
ast = Asset(100.0)
51+
except:
52+
print("Can't instantiate Asset!")
53+
54+
stocks.sort()
55+
bonds.sort()
56+
57+
for stock in stocks:
58+
print(stock)
59+
print("-----------")
60+
for bond in bonds:
61+
print(bond)

0 commit comments

Comments
 (0)