-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape_calculator.py
71 lines (51 loc) · 2.15 KB
/
shape_calculator.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
63
64
65
66
67
68
69
70
71
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def set_width(self, new_width): # setting new width of rectangle
self.width = new_width
def set_height(self, new_height): # setting new height of rectangle
self.height = new_height
def get_area(self): # getting the area of the rectangle
return self.width * self.height
def get_perimeter(self): # getting the perimrter
return 2 * (self.width + self.height)
def get_diagonal(self): # getting the diagonal
return ((self.width ** 2 + self.height ** 2) ** .5)
def get_picture(self): # getting the picture of the rectangle
if self.width >50 or self.height > 50:
return "Too big for picture."
row = ""
for i in range(self.height):
row += self.width * "*" + "\n"
return row
def get_amount_inside(self, shape): # getting amount of how many shapes can be fit inside
count = 0
if self.width > shape.width and self.height > shape.height:
count += self.get_area() // shape.get_area()
else:
return 0
return count
def __repr__(self): # getting the string representation
return f"Rectangle(width={self.width}, height={self.height})"
class Square(Rectangle):
def __init__(self,side,):
super().__init__(side, side)
def set_side(self, side): # setting new side of the square
super().set_width(side)
super().set_height(side)
def __repr__(self): # getting the string representation
return f"Square(side={self.width})"
rect = Rectangle(5, 5)
print(rect.get_area())
rect.set_width(3)
print(rect.get_perimeter())
print(rect)
sq = Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))