Skip to content

Commit 58fc71d

Browse files
authored
Add files via upload
1 parent 0f5689e commit 58fc71d

File tree

3 files changed

+59
-33
lines changed

3 files changed

+59
-33
lines changed

Diff for: cards.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Card:
1111

1212
# Normally you would only put function and name declarations
1313
# inside a class body, but this one just goes to show that any
14-
# statements inside he class body are executed the same way as
14+
# statements inside the class body are executed the same way as
1515
# any other statements.
1616
print('Here we are, declaring a class...')
1717

@@ -78,7 +78,7 @@ def outranks(self, other, trump=None):
7878

7979
print(f"Here is the directory of names in {c1}:")
8080
print(", ".join(dir(c1)))
81-
print(f"Here is the directory of names its class:")
81+
print("Here is the directory of names its class:")
8282
print(", ".join(dir(Card)))
8383

8484
# Cards have the ability to check if they outrank other cards.
@@ -89,22 +89,23 @@ def outranks(self, other, trump=None):
8989
print(f"Does {c1} outrank {c3} in notrump? {c1.outranks(c3)}")
9090
print(f"Does {c3} outrank {c1} in notrump? {c3.outranks(c1)}")
9191

92+
9293
# Watch how much easier thinking about computations becomes once the
9394
# concepts are sufficiently high level. One from your graded labs:
9495

95-
96-
def winning_card(cards_, trump=None, winner_so_far=None):
97-
for card in cards_:
96+
def winning_card(cards, trump=None):
97+
winner_so_far = None
98+
for card in cards:
9899
if card.outranks(winner_so_far, trump):
99100
winner_so_far = card
100101
return winner_so_far
101102

102103

103-
cards = [c1, c2, c3, c4]
104+
trick = [c1, c2, c3, c4]
104105
# Conversion of list to string uses repr, not str, for the list elements.
105-
print(f"\nCards played into the trick are: {str(cards)}.")
106-
# Usually you want to get a human readable representation of the list.
107-
print(f"Cards played are: {', '.join([str(c) for c in cards])}.")
108-
print(f"In notrump, trick is won by {winning_card(cards, None)}.")
109-
print(f"Hearts as trump, trick is won by {winning_card(cards, 'hearts')}.")
110-
print(f"Diamonds as trump, trick is won by {winning_card(cards, 'diamonds')}.")
106+
print(f"\nCards played into the trick are: {str(trick)}.")
107+
# Usually you want to get a human-readable representation of the list.
108+
print(f"Cards played are: {', '.join([str(c) for c in trick])}.")
109+
print(f"In notrump, trick is won by {winning_card(trick, None)}.")
110+
print(f"Hearts as trump, trick is won by {winning_card(trick, 'hearts')}.")
111+
print(f"Diamonds as trump, trick is won by {winning_card(trick, 'diamonds')}.")

Diff for: shape.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
rng = Random(12345)
1515

16+
1617
class Shape(ABC):
1718

1819
# A class attribute, so the same value is shared by everyone.
@@ -98,19 +99,19 @@ def perimeter(self):
9899

99100
class Scaled(Shape):
100101

101-
def __init__(self, other, scale):
102+
def __init__(self, client, scale):
102103
super().__init__()
103-
self.other = other
104+
self.client = client
104105
self.scale = scale
105106

106107
def name(self):
107-
return f"({self.other.name()} scaled by {self.scale})"
108+
return f"({self.client.name()} scaled by {self.scale})"
108109

109110
def area(self):
110-
return self.other.area() * self.scale * self.scale
111+
return self.client.area() * self.scale * self.scale
111112

112113
def perimeter(self):
113-
return self.other.perimeter() * self.scale
114+
return self.client.perimeter() * self.scale
114115

115116

116117
# Demonstrate the previous classes in action.
@@ -127,10 +128,10 @@ def __demo():
127128
print(f"Created: {r1}.")
128129
r2 = Rectangle(5, 5)
129130
print(f"Created: {r2}.")
130-
c = Circle(10)
131-
print(f"Created: {c}.")
132-
d = Circle(5)
133-
print(f"Created: {d}.")
131+
c1 = Circle(10)
132+
print(f"Created: {c1}.")
133+
c2 = Circle(5)
134+
print(f"Created: {c2}.")
134135

135136
# Next, we ask these objects what they think they are.
136137
print(f"\nObject r1 is of type {type(r1)}.")
@@ -145,24 +146,24 @@ def __demo():
145146
# back in horror seeing something like this. But go bananas!
146147

147148
# First, take the original names for safekeeping.
148-
tmp1, tmp2 = c.name, c.area
149+
tmp1, tmp2 = c1.name, c1.area
149150
# Lambdas can be defined to take no parameters, thus behaving
150151
# essentially as data.
151-
c.name = lambda: "Bob"
152-
c.area = lambda: rng.randint(1, 100)
153-
print(f"\nObject c is now: {c}") # Bob
154-
print(f"Object d is now: {d}") # behaves normally
152+
c1.name = lambda: "Bob"
153+
c1.area = lambda: rng.randint(1, 100)
154+
print(f"\nObject c1 is now: {c1}") # Bob
155+
print(f"Object c2 is now: {c2}") # behaves normally
155156

156157
# Restore the balance of the world.
157-
c.name, c.area = tmp1, tmp2
158-
print(f"Object c is now: {c}") # behaves normally again
159-
print(f"Object d is now: {d}") # behaves normally (still)
158+
c1.name, c1.area = tmp1, tmp2
159+
print(f"Object c1 is now: {c1}") # behaves normally again
160+
print(f"Object c2 is now: {c2}") # behaves normally (still)
160161

161162
# Demonstrate an object decorator in action.
162163
s1 = Scaled(r1, 2)
163164
print(f"Created: {s1}.")
164-
# Why not? Scalers are shapes, same way as any other shape.
165-
# Therefore they can be further decorated.
165+
# Why not? Scaled objects are shapes, same way as any other shape.
166+
# Therefore, they can be further decorated.
166167
s2 = Scaled(s1, 3)
167168
print(f"Created: {s2}.")
168169

Diff for: temperature.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ def __str__(self):
6363
def __repr__(self):
6464
return f"Temperature({self.K}, 'K')"
6565

66+
# To allow order and equality comparisons, define the following
67+
# dunder methods in your class for the six operators.
68+
69+
def __lt__(self, other): # <
70+
return self.K < other.K
71+
72+
def __gt__(self, other): # >
73+
return self.K > other.K
74+
75+
def __eq__(self, other): # ==
76+
return self.K == other.K
77+
78+
def __ne__(self, other): # !=
79+
return self.K != other.K
80+
81+
def __le__(self, other): # <=
82+
return self.K <= other.K
83+
84+
def __ge__(self, other): # >=
85+
return self.K >= other.K
86+
6687
# For temperatures, addition is meaningless, but temperatures
6788
# can be meaningfully subtracted from each other. Weird.
6889
def __sub__(self, other):
@@ -77,11 +98,14 @@ def __sub__(self, other):
7798

7899
def __demo():
79100
t1 = Temperature(30, 'C')
80-
print(f"Temperature is {t1.C:.1f} C, {t1.F:.1f} F, {t1.K:.1f} K.")
101+
print(f"Temperature t1 is {t1.C:.1f} C, {t1.F:.1f} F, {t1.K:.1f} K.")
81102
t2 = Temperature(100, 'F')
82-
print(f"Temperature is {t2.C:.1f} C, {t2.F:.1f} F, {t2.K:.1f} K.")
103+
print(f"Temperature t2 is {t2.C:.1f} C, {t2.F:.1f} F, {t2.K:.1f} K.")
83104
t3 = t1 - t2
84105
print(f"Their difference is {t3.K:.1f} K.")
106+
print(f"Does t1 < t2 ? {t1 < t2}")
107+
print(f"Does t1 > t2 ? {t1 > t2}")
108+
print(f"Does t1 == t1 ? {t1 == t1}")
85109
# And the crash.
86110
try:
87111
_ = Temperature(-400, 'C')

0 commit comments

Comments
 (0)