TelenkovDmitry 8 luni în urmă
părinte
comite
4f1547506d

+ 144 - 1
courses/python_oop/dunder_method/dunder_task.py

@@ -25,12 +25,155 @@ class Number:
         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()

+ 46 - 0
courses/python_oop/dunder_method/dunder_task_2.py

@@ -0,0 +1,46 @@
+# Магические методы сравнения
+# __eq__ - ==
+# __ne__ - !=
+# __lt__ <
+# __le__ <=
+# __gt__ >
+# __ge__ >=
+
+
+class Rectangle:
+
+    def __init__(self, a, b) -> None:
+        self.a = a
+        self.b = b
+
+    @property
+    def area(self):
+        return self.a * self.b
+
+    def __eq__(self, other):
+        if isinstance(other, Rectangle):
+            return self.a == other.a and self.b == other.b
+        
+    def __eq__(self, other):
+        if isinstance(other, Rectangle):
+            return self.area < other.area
+        elif isinstance(other, (int, float)):
+            return self.area < other
+        
+    def __le__(self, other):
+        return self==other or  self<other
+
+
+class Point:
+    
+    def __init__(self) -> None:
+        pass
+        
+
+
+def main():
+    pass
+
+
+if __name__ == '__main__':
+    main()