diff --git a/hometask7/task1.py b/hometask7/task1.py new file mode 100644 index 0000000..5934b8a --- /dev/null +++ b/hometask7/task1.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# nevl 2021 + +class Matrix: + + def __init__(self, data): + if not type(data) is list or not type(data[0]) is list: + print('Not a matrix. Try again') + return + + self.matrix_data = data + + def __str__(self): + result = '' + for iter in self.matrix_data: + s = '[' + for item in iter: + s += f'{item}\t' + result += f'{s}]\n' + return result + + def __add__(self, other): + data = list() + # check shape + if len(self.matrix_data) != len(other.matrix_data) or len(self.matrix_data[0]) != len(other.matrix_data[0]): + return None + + # calculate + for i in range(len(self.matrix_data)): + data.append(list()) + for j in range(len(self.matrix_data[0])): + data[i].append(self.matrix_data[i][j] + other.matrix_data[i][j]) + + return Matrix(data) + + +if __name__ == '__main__': + mart1 = Matrix([[1, 2, 3], [3, 4, 5]]) + print(mart1) + + mart2 = Matrix([[1.1, 2.2, 3.3], [3.3, 4.4, 5.5]]) + print(mart2) + + print(mart1 + mart2) diff --git a/hometask7/task2.py b/hometask7/task2.py new file mode 100644 index 0000000..665b8af --- /dev/null +++ b/hometask7/task2.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# nevl 2021 + +from abc import ABC, abstractmethod + + +class dress(ABC): + + def __init__(self, size): + self.size = size + + @abstractmethod + def cloth_rate(self): + pass + + +class coat(dress): + + @property + def cloth_rate(self): + return self.size / 6.5 + 0.5 + + +class suit(dress): + + @property + def cloth_rate(self): + return self.size * 2 + 0.3 + + +if __name__ == '__main__': + c1 = coat(50) + print(c1.cloth_rate) + + s1 = suit(1.75) + print(s1.cloth_rate) + + c2 = coat(54) + print(c2.cloth_rate) + + s2 = suit(1.85) + print(s2.cloth_rate) + + print(f'Cloth for all dresses = {s1.cloth_rate + s2.cloth_rate + c1.cloth_rate + c2.cloth_rate}') diff --git a/hometask7/task3.py b/hometask7/task3.py new file mode 100644 index 0000000..c7e9072 --- /dev/null +++ b/hometask7/task3.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# nevl 2021 + +class Cell: + + def __init__(self, count): + self.count = count + + def __add__(self, other): + return Cell(self.count + other.count) + + def __sub__(self, other): + t = self.count - other.count + if t < 0: + raise OverflowError + return Cell(t) + + def __mul__(self, other): + return Cell(self.count * other.count) + + def __truediv__(self, other): + return Cell(self.count // other.count) + + def make_order(self, line_count): + s = '' + for _ in range(self.count // line_count): + s += '*'*line_count + '\n' + s += '*'*(self.count % line_count) + '\n' + return s + + +if __name__ == '__main__': + cell1 = Cell(31) + cell2 = Cell(35) + cell3 = Cell(27) + + for it in [cell1, cell2, cell3]: + print(it.make_order(9)) + + try: + cell4 = cell1 - cell2 + print(cell4.make_order(9)) + except: + print('Error') + + try: + cell5 = cell2 - cell3 + print(cell5.make_order(9)) + except: + print('Error')