diff --git a/task1.py b/task1.py new file mode 100644 index 0000000..9c356b1 --- /dev/null +++ b/task1.py @@ -0,0 +1,35 @@ +class Matrix: + def __init__(self, matrix_list): + if isinstance(matrix_list, list): + for matrix_row in matrix_list: + if not isinstance(matrix_list, list): + return 'Error' + else: + return 'Error' + self.matrix_list = matrix_list + + def __str__(self): + for matrix_row in self.matrix_list: + for i in matrix_row: + print(i, end=' ') + print('') + return '' + + def __add__(self, other): + result_list = [] + for row_num, matrix_row in enumerate(self.matrix_list): + new_row = [] + for i, num in enumerate(matrix_row): + new_row.append(num + other.matrix_list[row_num][i]) + result_list.append(new_row) + # print(result_list) + return Matrix(result_list) + + + +matrix1 = [[1, 2, 3, 4], [5, 4, 3, 2], [4, 6, 7, 9]] +matrix2 = [[1, 2, -3, -4], [5, -4, -3, 2], [2, 3, 1, 0]] + +new_matrix1 = Matrix(matrix1) +new_matrix2 = Matrix(matrix2) +print(new_matrix1 + new_matrix2) diff --git a/task2.py b/task2.py new file mode 100644 index 0000000..2449218 --- /dev/null +++ b/task2.py @@ -0,0 +1,42 @@ +from abc import ABC, abstractmethod + +class Clothes(ABC): + + def __init__(self, name): + self.name = name + + @abstractmethod + def material_count(self): + pass + + def __str__(self): + return f'Общий расход по {self.name} = {self.material_count}' + + +class Palto(Clothes): + + def __init__(self, name, v): + Clothes.__init__(self, name) + self.v = v + + @property + def material_count(self): + return self.v / 6.5 + 0.5 + + +class Costum(Clothes): + + def __init__(self, name, h): + Clothes.__init__(self, name) + self.h = h + + @property + def material_count(self): + return 2 * self.h + 0.3 + + +clother_1 = Palto('Пальто', 48) +clother_2 = Costum('Костюм', 175) + +sum_materil = clother_1.material_count + clother_2.material_count +print(sum_materil) \ No newline at end of file diff --git a/task3.py b/task3.py new file mode 100644 index 0000000..e3e44a1 --- /dev/null +++ b/task3.py @@ -0,0 +1,42 @@ +class Element: + + def __init__(self, cells): + if cells <= 0: + print('кол-во ячеек должно быть положительным числом!') + return False + self.cells = cells + + def __add__(self, other): + return Element(self.cells + other.cells) + + def __sub__(self, other): + if self.cells > other.cells: + return Element(self.cells - other.cells) + print('Недостаточно ячеек для вычитания') + return False; + + def __mul__(self, other): + return Element(self.cells * other.cells) + + def __truediv__(self, other): + if other.cells == 0: + print('Нельзя делить на пустую клетку') # скорее всего это условие несработает никогда, потому что другие + # методы не генерируют пустые клетки, но пусть будет, вдруг измениться что-то в программе + return False + return Element(round(self.cells / other.cells, 0)) + + def make_order(self, row_len): + str_cells = '' + a = 0 + while(a < self.cells): + str_cells += '*' + a += 1 + if a % row_len == 0: + str_cells += '\n' + return str_cells + + +element1 = Element(5) +element2 = Element(3) +element3 = element1 + element2 +print(element3.make_order(5)) \ No newline at end of file