-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathshapes.py
More file actions
57 lines (37 loc) · 1.15 KB
/
shapes.py
File metadata and controls
57 lines (37 loc) · 1.15 KB
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
from math import pi
from typing import Protocol
class Shape(Protocol):
def area(self) -> float: ...
def perimeter(self) -> float: ...
class Circle:
def __init__(self, radius: float) -> None:
self.radius = radius
def area(self) -> float:
return pi * self.radius**2
def perimeter(self) -> float:
return 2 * pi * self.radius
class Square:
def __init__(self, side: float) -> None:
self.side = side
def area(self) -> float:
return self.side**2
def perimeter(self) -> float:
return 4 * self.side
class Rectangle:
def __init__(self, length: float, width: float) -> None:
self.length = length
self.width = width
def area(self) -> float:
return self.length * self.width
def perimeter(self) -> float:
return 2 * (self.length + self.width)
def describe_shape(shape: Shape) -> None:
print(f"{type(shape).__name__}")
print(f" Area: {shape.area():.2f}")
print(f" Perimeter: {shape.perimeter():.2f}")
circle = Circle(3)
square = Square(5)
rectangle = Rectangle(4, 5)
describe_shape(circle)
describe_shape(square)
describe_shape(rectangle)