dunder_task.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. class Vector:
  2. def __init__(self, x, y):
  3. self.x = x
  4. self.y = y
  5. def __add__(self, other):
  6. if isinstance(other, Number):
  7. return Vector(self.x + other.value, self.y + other.value)
  8. def __str__(self):
  9. return f"Vector({self.x},{self.y})"
  10. class Number:
  11. def __init__(self, value) -> None:
  12. self.value = value
  13. def __radd__(self, other):
  14. if isinstance(other, Vector):
  15. return Number(other.x + other.y + self.value)
  16. def __str__(self) -> str:
  17. return f"Number({self.value})"
  18. class Rectangle:
  19. def __init__(self, width, height) -> None:
  20. self.width = width
  21. self.height = height
  22. def __add__(self, other):
  23. return Rectangle(self.width + other.width, self.height + other.height)
  24. def __str__(self):
  25. return f"Rectangle({self.width}x{self.height})"
  26. class Vector:
  27. def __init__(self, x, y):
  28. self.x = x
  29. self.y = y
  30. def __repr__(self):
  31. return f"Vector({self.x}, {self.y})"
  32. def __mul__(self, other):
  33. return self.x * other.x + self.y * other.y
  34. class Order:
  35. def __init__(self, cart: list, customer):
  36. self.cart = cart
  37. self.customer = customer
  38. def __add__(self, other):
  39. new_cart = self.cart.copy()
  40. new_cart.append(other)
  41. return Order(new_cart, self.customer)
  42. def __radd__(self, other):
  43. new_cart = self.cart.copy()
  44. new_cart.insert(0, other)
  45. return Order(new_cart, self.customer)
  46. def __sub__(self, other):
  47. new_cart = self.cart.copy()
  48. if other in self.cart:
  49. new_cart.remove(other)
  50. return Order(new_cart, self.customer)
  51. def __rsub__(self, other):
  52. return self.__sub__(other)
  53. class Vector:
  54. def __init__(self, *args):
  55. foo = []
  56. for i in args:
  57. if isinstance(i, int):
  58. foo.append(i)
  59. self.values = sorted(foo)
  60. def __str__(self):
  61. if self.values:
  62. foo = [str(i) for i in self.values]
  63. return f"Вектор({', '.join(foo)})"
  64. else:
  65. return "Пустой вектор"
  66. def __add__(self, other):
  67. if isinstance(other, int):
  68. foo = [i+other for i in self.values]
  69. return Vector(*foo)
  70. elif isinstance(other, Vector):
  71. if len(other.values) == len(self.values):
  72. foo = [sum(i) for i in zip(self.values, other.values)]
  73. return Vector(*foo)
  74. else:
  75. print("Сложение векторов разной длины недопустимо")
  76. else:
  77. print(f"Вектор нельзя сложить с {other}")
  78. def __mul__(self, other):
  79. if isinstance(other, int):
  80. foo = [i * other for i in self.values]
  81. return Vector(*foo)
  82. elif isinstance(other, Vector):
  83. if len(other.values) == len(self.values):
  84. foo = [i[0]*i[1] for i in zip(self.values, other.values)]
  85. return Vector(*foo)
  86. else:
  87. print("Умножение векторов разной длины недопустимо")
  88. else:
  89. print(f"Вектор нельзя умножать с {other}")
  90. def main():
  91. '''
  92. order = Order(['banana', 'apple'], 'Гена Букин')
  93. order_2 = order + 'orange'
  94. assert order.cart == ['banana', 'apple']
  95. assert order.customer == 'Гена Букин'
  96. assert order_2.cart == ['banana', 'apple', 'orange']
  97. order = 'mango' + order
  98. assert order.cart == ['mango', 'banana', 'apple']
  99. order = 'ice cream' + order
  100. assert order.cart == ['ice cream', 'mango', 'banana', 'apple']
  101. order = order - 'banana'
  102. assert order.cart == ['ice cream', 'mango', 'apple']
  103. order3 = order - 'banana'
  104. assert order3.cart == ['ice cream', 'mango', 'apple']
  105. order = order - 'mango'
  106. assert order.cart == ['ice cream', 'apple']
  107. order = 'lime' - order
  108. assert order.cart == ['ice cream', 'apple']
  109. print('Good')
  110. '''
  111. '''
  112. r1 = Rectangle(5, 10)
  113. assert r1.width == 5
  114. assert r1.height == 10
  115. print(r1)
  116. r2 = Rectangle(20, 5)
  117. assert r2.width == 20
  118. assert r2.height == 5
  119. print(r2)
  120. r3 = r2 + r1
  121. assert isinstance(r3, Rectangle)
  122. assert r3.width == 25
  123. assert r3.height == 15
  124. print(r3)
  125. '''
  126. '''
  127. v = Vector(2, 3)
  128. num = Number(5)
  129. print(num + v)
  130. '''
  131. if __name__ == '__main__':
  132. main()