-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathshapes.py
62 lines (45 loc) · 1.65 KB
/
shapes.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
60
61
62
class BaseShape:
def area(self):
raise NotImplementedError
class Rectangle(BaseShape):
def __init__(self, length: int, width: int, **kwargs):
self.length = length
self.width = width
super().__init__(**kwargs)
def area(self) -> int:
return self.length * self.width
def perimeter(self) -> int:
return 2 * self.length + 2 * self.width
class Square(Rectangle):
def __init__(self, length: int, **kwargs):
super().__init__(length, length, **kwargs)
class Triangle(BaseShape):
def __init__(self, base: int, height: int, **kwargs):
self.base = base
self.height = height
super().__init__(**kwargs)
def tri_area(self) -> float:
return 0.5 * self.base * self.height
class RightPyramid(Square, Triangle):
def __init__(self, base: int, slant_height: int):
super().__init__(base=base, height=slant_height, length=base)
self.base = base
self.slant_height = slant_height
def pyramid_area(self):
base_area = self.area()
side_area = self.tri_area()
return side_area * 4 + base_area
if __name__ == '__main__':
rectangle = Rectangle(2, 4)
print("Rectangle:", rectangle.length, rectangle.width)
print("Area:", rectangle.area())
print("Perimeter:", rectangle.perimeter())
square = Square(5)
print("Square:", square.length)
print("Area:", square.area())
print("Perimeter:", square.perimeter())
triangle = Triangle(4, 6)
print("triangle area:", triangle.tri_area())
pyramid = RightPyramid(10, 12)
print(RightPyramid.__mro__)
print("pyramid area", pyramid.pyramid_area())