Advertisement
Alex-Flexer

Untitled

Sep 17th, 2023
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. from math import sqrt, pi
  2.  
  3.  
  4. class Compare:
  5. _area: int
  6. _type: type
  7.  
  8. def check_class(funk):
  9. def wrapper(self_obj, other_obj):
  10. if not isinstance(other_obj, self_obj._type):
  11. raise TypeError(f"Variable is not {self_obj._type.__name__}")
  12. return funk(self_obj, other_obj)
  13. return wrapper
  14.  
  15. def __init__(self, area, type_) -> None:
  16. self.area = area
  17. self._type = type_
  18.  
  19. @check_class
  20. def __eq__(self, other) -> bool:
  21. return self.get_space() == other.get_space()
  22.  
  23. @check_class
  24. def __ne__(self, other) -> bool:
  25. return self.get_space() != other.get_space()
  26.  
  27. @check_class
  28. def __lt__(self, other) -> bool:
  29. return self.get_space() < other.get_space()
  30.  
  31. @check_class
  32. def __le__(self, other) -> bool:
  33. return self.get_space() <= other.get_space()
  34.  
  35. @check_class
  36. def __gt__(self, other) -> bool:
  37. return self.get_space() > other.get_space()
  38.  
  39. @check_class
  40. def __ge__(self, other) -> bool:
  41. return self.get_space() >= other.get_space()
  42.  
  43.  
  44. class Rectangle (Compare):
  45. length: int
  46. width: int
  47.  
  48. def get_space(self) -> int:
  49. return self.length * self.width
  50.  
  51. def __init__(self, a, b) -> None:
  52. self.length = a
  53. self.width = b
  54. super().__init__(self.get_space(), self.__class__)
  55.  
  56. def __str__(self) -> str:
  57. return f'length: {self.length}, width: {self.width}'
  58.  
  59. def get_length(self) -> int:
  60. return self.length
  61.  
  62. def get_width(self) -> int:
  63. return self.width
  64.  
  65. def get_perimeter(self) -> int:
  66. return 2 * (self.length + self.width)
  67.  
  68.  
  69. class Triangle (Compare):
  70. a: int
  71. b: int
  72. c: int
  73. def __init__(self, a, b, c) -> None:
  74. self.a = max(a, b, c)
  75. self.c = min(a, b, c)
  76. self.b = a + b + c - self.a - self.c
  77.  
  78. super().__init__(self.get_space(), self.__class__)
  79.  
  80. def __str__(self) -> str:
  81. return f'A: {self.a}, B: {self.b}, C: {self.c}'
  82.  
  83. def get_sides(self) -> int:
  84. return (self.a, self.b, self.c)
  85.  
  86. def get_perimeter(self) -> int:
  87. return sum(self.get_sides())
  88.  
  89. def get_space(self) -> int:
  90. p = self.get_perimeter() / 2
  91. return sqrt(p * (p - self.a) * (p - self.b) * (p - self.c))
  92.  
  93.  
  94. class Circle (Compare):
  95. radius: int
  96.  
  97. def __init__(self, r) -> None:
  98. self.radius = r
  99. super().__init__(self.get_space(), self.__class__)
  100.  
  101. def __str__(self) -> str:
  102. return f'Radius: {self.radius}'
  103.  
  104. def get_radius(self):
  105. return self.radius
  106.  
  107. def get_perimeter(self) -> int:
  108. return 2 * self.radius * pi
  109.  
  110. def get_space(self) -> int:
  111. return (self.radius ** 2) * pi
  112.  
  113.  
  114. class Square (Rectangle):
  115. def __init__(self, a) -> None:
  116. Rectangle.__init__(self, a, a)
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement