-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
59 lines (38 loc) · 1.36 KB
/
example.py
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
import math
class RoundHole:
"""RoundHole is compatible with RoundPegs only."""
def __init__(self, radius):
self._radius = radius
def get_radius(self):
return self._radius
def fits(self, peg):
return self.get_radius() >= peg.get_radius()
class RoundPeg:
"""RoundPegs are compatible with RoundHoles."""
def __init__(self, radius):
self._radius = radius
def get_radius(self):
return self._radius
class SquarePeg:
"""SquarePegs are not compatible with RoundHoles."""
def __init__(self, width):
self._width = width
def get_width(self):
return self._width
class SquarePegAdapter(RoundPeg):
"""SquarePegAdapter allows fitting square pegs into round holes."""
def __init__(self, peg):
self._peg = peg
def get_radius(self):
return self._peg.get_width() * math.sqrt(2) / 2
if __name__ == "__main__":
hole = RoundHole(5)
rpeg = RoundPeg(5)
print(hole.fits(rpeg)) # True
small_sqpeg = SquarePeg(5)
large_sqpeg = SquarePeg(10)
# hole.fits(small_sqpeg) # TypeError: 'SquarePeg' object cannot be interpreted as an integer
small_sqpeg_adapter = SquarePegAdapter(small_sqpeg)
large_sqpeg_adapter = SquarePegAdapter(large_sqpeg)
print(hole.fits(small_sqpeg_adapter)) # True
print(hole.fits(large_sqpeg_adapter)) # False