diff --git a/task7_1.py b/task7_1.py new file mode 100644 index 0000000..e70c5d1 --- /dev/null +++ b/task7_1.py @@ -0,0 +1,35 @@ +def list_sum(l_1, l_2): + return [x + y for x, y in zip(l_1, l_2)] + + +class Matrix: + def __init__(self, matrix): + self.matrix = matrix + + def __str__(self): + res = '' + for x in self.matrix: + for y in x: + res += f"{y : ^10}" + res += '\n' + return res + + def __add__(self, other): + res = [] + try: + for i, x in enumerate(self.matrix): + res.append(list_sum(x, other.matrix[i])) + except IndexError: + return Matrix(res) + return Matrix(res) + + +m_1 = Matrix([[31, 22, 2], [54, 6, 9], [8, 5, -1]]) +m_2 = Matrix([[11, 5, -1], [4, 26, 4], [-4, 66, 45], [1, 4, 5]]) +print(m_1) +print("-" * 30) +print(m_2) +print("-" * 30) +print("Сумма матриц:") +print(m_1 + m_2) + diff --git a/task7_2.py b/task7_2.py new file mode 100644 index 0000000..39cbe84 --- /dev/null +++ b/task7_2.py @@ -0,0 +1,38 @@ +from abc import ABC, abstractmethod + + +class Clothes(ABC): + def __init__(self, size): + self.size = size + + @property + @abstractmethod + def tissue_consumption(self): + pass + + def __add__(self, other): + return self.tissue_consumption + other.tissue_consumption + + +class Coat(Clothes): + + @property + def tissue_consumption(self): + return round((self.size / 6.5) + 0.5) + + +class Jacket(Clothes): + + @property + def tissue_consumption(self): + return round((self.size * 2) + 0.3) / 100 + + +cloth = Clothes +coat = Coat(8) +print(f"{coat.tissue_consumption}м ткани") +print("-" * 30) +jacket = Jacket(165) +print(f"{jacket.tissue_consumption}м ткани") +print("-" * 30) +print(f"{coat + jacket}м ткани") diff --git a/task7_3.py b/task7_3.py new file mode 100644 index 0000000..22d98bd --- /dev/null +++ b/task7_3.py @@ -0,0 +1,52 @@ +from random import randint + + +def get_random_char(): + return chr(randint(33, 44)) + + +class Cell: + def __init__(self, cells): + self.cells = cells + + def __str__(self): + return f"Я клетка! Во мне {self.cells} ячеек" + + def __add__(self, other): + return self.cells + other.cells + + def __sub__(self, other): + if self.cells - other.cells < 0: + print(f"Разница не может быть отрицательна!") + return 0 + else: + return self.cells - other.cells + + def __mul__(self, other): + return self.cells * other.cells + + def __truediv__(self, other): + return self.cells // other.cells + + def make_order(self, num): + sym = get_random_char() + return '\n'.join([sym * num for x in range(0, self.cells // num)]) \ + + f"\n{sym * (self.cells % num)}" + + +cell_1 = Cell(18) +cell_2 = Cell(34) +print(cell_1) +print(cell_2) +print() +print(cell_1.make_order(5)) +print() +print(cell_1.make_order(8)) +print() +print(cell_2.make_order(9)) +print() +print(cell_1 + cell_2) +print(cell_1 - cell_2) +print(cell_2 - cell_1) +print(cell_2 / cell_1) +print(cell_2 * cell_1)