Skip to content
Open

HW7 #116

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lesson-7/task1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import random


class MatrixException(Exception):
pass


class Matrix:
def __init__(self, matrix):
self.matrix = matrix
self.rows = len(matrix)
self.cols = len(matrix[0])

def __str__(self):
return "\n".join([' '.join(map(str, tmp)) for tmp in self.matrix])

def __add__(self, other):
if self.rows != other.rows or self.cols != other.cols:
raise MatrixException('Matrices has different size')
# res = [map(sum, zip(*tmp)) for tmp in zip(self.matrix, other.matrix)]
res = generate_matrix(self.rows, self.cols, nulls=True)
for i in range(self.rows):
for j in range(self.cols):
res[i][j] = self.matrix[i][j] + other.matrix[i][j]
return Matrix(res)


def generate_matrix(rows: int, cols: int, *, nulls: bool = False) -> list:
matrix = []
for i in range(rows):
matrix.append([random.randint(0, 100) if not nulls else 0 for j in range(cols)])
return matrix


matrix1 = Matrix(generate_matrix(
int(input('Enter rows matrix 1: ')),
int(input('Enter cols matrix 1: ')),
))

matrix2 = Matrix(generate_matrix(
int(input('Enter rows matrix 2: ')),
int(input('Enter cols matrix 2: ')),
))

print('Matrix 1')
print(matrix1)
print('Matrix 2')
print(matrix2)
try:
print('Matrix sum:')
print(matrix1 + matrix2)
except MatrixException as e:
print(e)
29 changes: 29 additions & 0 deletions lesson-7/task2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from abc import ABC, abstractmethod


class Cloth(ABC):
@abstractmethod
def calc(self):
pass


class Coat(Cloth):
def __init__(self, v):
self.__v = v

@property
def calc(self):
return self.__v / 6.5 + 0.5


class Suit(Cloth):
def __init__(self, h):
self.__h = h

@property
def calc(self):
return self.__h * 2 + 0.3


print(Coat(10).calc)
print(Suit(8).calc)
47 changes: 47 additions & 0 deletions lesson-7/task3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class CellSubException(Exception):
pass


class CellTruedivException(Exception):
pass


class Cell:
def __init__(self, count: int):
self.count = count

def __add__(self, other):
return Cell(self.count + other.count)

def __sub__(self, other):
if self.count < other.count:
raise CellSubException('sub error')
return Cell(self.count - other.count)

def __mul__(self, other):
return self.count * other.count

def __truediv__(self, other):
if other.count == 0:
raise CellTruedivException('division by zero')
if self.count // other.count == 0:
raise CellTruedivException('zero result')
return Cell(self.count // other.count)

def make_order(self, in_row: int):
for i in range(self.count):
print('*', end='')
if (i+1) % in_row == 0:
print('')
print('')


c1 = Cell(20)
c2 = Cell(8)

print('SUM:', c1 + c2)
print('SUB:', c1 - c2)
print('MUL:', c1 * c2)
print('TRUEDIV:', c1 / c2)

c1.make_order(4)