123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- class Vector:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def __add__(self, other):
- if isinstance(other, Number):
- return Vector(self.x + other.value, self.y + other.value)
- def __str__(self):
- return f"Vector({self.x},{self.y})"
- class Number:
- def __init__(self, value) -> None:
- self.value = value
- def __radd__(self, other):
- if isinstance(other, Vector):
- return Number(other.x + other.y + self.value)
-
- def __str__(self) -> str:
- return f"Number({self.value})"
-
- class Rectangle:
- def __init__(self, width, height) -> None:
- self.width = width
- self.height = height
- def __add__(self, other):
- return Rectangle(self.width + other.width, self.height + other.height)
-
- def __str__(self):
- return f"Rectangle({self.width}x{self.height})"
- class Vector:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def __repr__(self):
- return f"Vector({self.x}, {self.y})"
- def __mul__(self, other):
- return self.x * other.x + self.y * other.y
-
- class Order:
- def __init__(self, cart: list, customer):
- self.cart = cart
- self.customer = customer
- def __add__(self, other):
- new_cart = self.cart.copy()
- new_cart.append(other)
- return Order(new_cart, self.customer)
-
- def __radd__(self, other):
- new_cart = self.cart.copy()
- new_cart.insert(0, other)
- return Order(new_cart, self.customer)
- def __sub__(self, other):
- new_cart = self.cart.copy()
- if other in self.cart:
- new_cart.remove(other)
- return Order(new_cart, self.customer)
- def __rsub__(self, other):
- return self.__sub__(other)
- class Vector:
- def __init__(self, *args):
- foo = []
- for i in args:
- if isinstance(i, int):
- foo.append(i)
- self.values = sorted(foo)
- def __str__(self):
- if self.values:
- foo = [str(i) for i in self.values]
- return f"Вектор({', '.join(foo)})"
- else:
- return "Пустой вектор"
- def __add__(self, other):
- if isinstance(other, int):
- foo = [i+other for i in self.values]
- return Vector(*foo)
- elif isinstance(other, Vector):
- if len(other.values) == len(self.values):
- foo = [sum(i) for i in zip(self.values, other.values)]
- return Vector(*foo)
- else:
- print("Сложение векторов разной длины недопустимо")
- else:
- print(f"Вектор нельзя сложить с {other}")
- def __mul__(self, other):
- if isinstance(other, int):
- foo = [i * other for i in self.values]
- return Vector(*foo)
- elif isinstance(other, Vector):
- if len(other.values) == len(self.values):
- foo = [i[0]*i[1] for i in zip(self.values, other.values)]
- return Vector(*foo)
- else:
- print("Умножение векторов разной длины недопустимо")
- else:
- print(f"Вектор нельзя умножать с {other}")
- def main():
-
- '''
- order = Order(['banana', 'apple'], 'Гена Букин')
- order_2 = order + 'orange'
- assert order.cart == ['banana', 'apple']
- assert order.customer == 'Гена Букин'
- assert order_2.cart == ['banana', 'apple', 'orange']
- order = 'mango' + order
- assert order.cart == ['mango', 'banana', 'apple']
- order = 'ice cream' + order
- assert order.cart == ['ice cream', 'mango', 'banana', 'apple']
- order = order - 'banana'
- assert order.cart == ['ice cream', 'mango', 'apple']
- order3 = order - 'banana'
- assert order3.cart == ['ice cream', 'mango', 'apple']
- order = order - 'mango'
- assert order.cart == ['ice cream', 'apple']
- order = 'lime' - order
- assert order.cart == ['ice cream', 'apple']
- print('Good')
- '''
- '''
- r1 = Rectangle(5, 10)
- assert r1.width == 5
- assert r1.height == 10
- print(r1)
- r2 = Rectangle(20, 5)
- assert r2.width == 20
- assert r2.height == 5
- print(r2)
- r3 = r2 + r1
- assert isinstance(r3, Rectangle)
- assert r3.width == 25
- assert r3.height == 15
- print(r3)
- '''
- '''
- v = Vector(2, 3)
- num = Number(5)
- print(num + v)
- '''
-
- if __name__ == '__main__':
- main()
|