-
Notifications
You must be signed in to change notification settings - Fork 73
ДЗ Урок 7 #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iezkiil
wants to merge
1
commit into
Kartavec:master
Choose a base branch
from
iezkiil:lesson_7
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ДЗ Урок 7 #127
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
""" | ||
1. Реализовать класс Matrix (матрица). Обеспечить перегрузку конструктора класса (метод __init__()), | ||
который должен принимать данные (список списков) для формирования матрицы. | ||
Подсказка: матрица — система некоторых математических величин, расположенных в виде прямоугольной схемы. | ||
Примеры матриц: 3 на 2, 3 на 3, 2 на 4. | ||
|
||
31 32 3 5 32 3 5 8 3 | ||
37 43 2 4 6 8 3 7 1 | ||
51 86 -1 64 -8 | ||
Следующий шаг — реализовать перегрузку метода __str__() для вывода матрицы в привычном виде. | ||
Далее реализовать перегрузку метода __add__() для реализации операции сложения двух объектов класса | ||
Matrix (двух матриц). Результатом сложения должна быть новая матрица. | ||
Подсказка: сложение элементов матриц выполнять поэлементно — первый элемент первой строки первой матрицы | ||
складываем с первым элементом первой строки второй матрицы и т.д. | ||
""" | ||
|
||
|
||
class Matrix: | ||
def __init__(self, *matr): | ||
self.matr = matr | ||
print(f'{self.matr} введенные данные имеют тип: {type(self.matr)}') | ||
|
||
def __str__(self): | ||
out_put = '' | ||
for el in self.matr: | ||
el = str(el) | ||
el = el.replace(',', '').replace(']', '').replace('[', '') | ||
out_put = out_put + '\n' + el | ||
return out_put | ||
|
||
|
||
|
||
def __add__(self, other): | ||
if len(self.matr) != len(other.matr): #проверяем возможность сложения матиц | ||
print('матрицы не равны по количеству строк. сложение не возможно!') | ||
for row_elem in range(len(self.matr)): | ||
if len(self.matr[row_elem]) != len(other.matr[row_elem]): | ||
print('матрицы не равны по количеству столбцов. сложение не возможно!') | ||
break | ||
sum_matrix = self.matr | ||
for row in range(len(self.matr)): | ||
for column in range(len(self.matr[row])): | ||
sum_matrix[row][column] = self.matr[row][column] + other.matr[row][column] | ||
return sum_matrix | ||
|
||
|
||
matrix_1 = Matrix([1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]) #подумал, что кортеж списков будет работать | ||
print(matrix_1) #быстрее списка списков | ||
|
||
matrix_2 = Matrix([2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]) | ||
print(matrix_2) | ||
|
||
matrix_sum = matrix_1 + matrix_2 | ||
print(type(matrix_sum)) | ||
print(f'сумма двух матриц {matrix_sum}') | ||
|
||
|
||
print('------------------------------------------------------------------------------------------') | ||
|
||
"""2. Реализовать проект расчёта суммарного расхода ткани на производство одежды. Основная сущность | ||
(класс) этого проекта — одежда, которая может иметь определённое название. К типам одежды в этом | ||
проекте относятся пальто и костюм. У этих типов одежды существуют параметры: размер (для пальто) | ||
и рост (для костюма). Это могут быть обычные числа: V и H, соответственно. | ||
Для определения расхода ткани по каждому типу одежды использовать формулы: для пальто (V/6.5 + 0.5), | ||
для костюма (2*H + 0.3). Проверить работу этих методов на реальных данных. | ||
Реализовать общий подсчет расхода ткани. Проверить на практике полученные на этом уроке знания: | ||
реализовать абстрактные классы для основных классов проекта, проверить на практике работу | ||
декоратора @property. | ||
""" | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
class Roba: | ||
def __init__(self, name): | ||
self.name = name | ||
|
||
@abstractmethod | ||
def fabrik_spend(self): | ||
pass | ||
|
||
def __add__(self, other): | ||
total_spend = self.spend + other.spend | ||
return total_spend | ||
|
||
class Coat(Roba): | ||
def __init__(self, name, size): | ||
super().__init__(name) | ||
self.size = size | ||
|
||
@property | ||
def fabrik_spend(self): | ||
self.spend = self.size / 6.5 + 0.5 | ||
return self.spend | ||
|
||
|
||
class Suit(Roba): | ||
def __init__(self, name, heigh): | ||
super().__init__(name) | ||
self.heigh = heigh | ||
|
||
@property | ||
def fabrik_spend(self): | ||
self.spend = self.heigh * 2 + 0.3 | ||
return self.spend | ||
|
||
|
||
coat_1 = Coat('coat #1', 56) | ||
|
||
print(f'расход ткани на пальто {coat_1.fabrik_spend:.2f}') | ||
|
||
suit_1 = Suit('suit #1', 1.8) | ||
|
||
print(f'расход ткани на костюм {suit_1.fabrik_spend:.2f}') | ||
|
||
print(f'общий расход ткани составил {coat_1 + suit_1:.2f}') | ||
|
||
print('------------------------------------------------------------------------------------------') | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выполнено |
||
|
||
"""3. Реализовать программу работы с органическими клетками, состоящими из ячеек. Необходимо | ||
создать класс Клетка. В его конструкторе инициализировать параметр, соответствующий количеству | ||
ячеек клетки (целое число). В классе должны быть реализованы методы перегрузки арифметических | ||
операторов: сложение (__add__()), вычитание (__sub__()), умножение (__mul__()), | ||
деление (__truediv__()). Данные методы должны применяться только к клеткам и выполнять увеличение, | ||
уменьшение, умножение и целочисленное (с округлением до целого) деление клеток, соответственно. | ||
|
||
Сложение. Объединение двух клеток. При этом число ячеек общей клетки должно равняться сумме ячеек | ||
исходных двух клеток. | ||
|
||
Вычитание. Участвуют две клетки. Операцию необходимо выполнять только если разность количества | ||
ячеек двух клеток больше нуля, иначе выводить соответствующее сообщение. | ||
|
||
Умножение. Создаётся общая клетка из двух. Число ячеек общей клетки определяется как произведение | ||
количества ячеек этих двух клеток. | ||
|
||
Деление. Создаётся общая клетка из двух. Число ячеек общей клетки определяется как целочисленное | ||
деление количества ячеек этих двух клеток. | ||
|
||
В классе необходимо реализовать метод make_order(), принимающий экземпляр класса и количество | ||
ячеек в ряду. Данный метод позволяет организовать ячейки по рядам. | ||
Метод должен возвращать строку вида *****\n*****\n*****..., где количество ячеек между \n равно | ||
переданному аргументу. Если ячеек на формирование ряда не хватает, то в последний ряд записываются | ||
все оставшиеся. | ||
Например, количество ячеек клетки равняется 12, количество ячеек в ряду — 5. | ||
Тогда метод make_order() вернёт строку: *****\n*****\n**. | ||
Или, количество ячеек клетки равняется 15, количество ячеек в ряду — 5. | ||
Тогда метод make_order() вернёт строку: *****\n*****\n*****.""" | ||
|
||
class Cell: | ||
def __init__(self, quantity, grid): | ||
if type(quantity) == int: | ||
self.quantity = quantity | ||
else: | ||
print('Количество ячеек в клетке должно быть целым числом') | ||
self.grid = grid | ||
|
||
|
||
def __add__(self, other): | ||
total_quantity = self.quantity + other.quantity | ||
return total_quantity | ||
|
||
|
||
def __mul__(self, other): | ||
total_quantity = self.quantity * other.quantity | ||
return total_quantity | ||
|
||
def __sub__(self, other): | ||
if self.quantity < other.quantity: | ||
print('Количество ячеек в клетке слева недостаточно для вычитания') | ||
else: | ||
total_quantity = self.quantity - other.quantity | ||
return total_quantity | ||
|
||
|
||
def __truediv__(self, other): | ||
total_quantity = self.quantity // other.quantity | ||
return total_quantity | ||
|
||
|
||
def make_order(self): | ||
for elem in range(self.quantity // self.grid): | ||
print('*' * self.grid) | ||
print('*' * (self.quantity % self.grid)) | ||
print('-' * self.grid) #линия для наглядности разделения | ||
|
||
|
||
cell_1 = Cell(6, 5) | ||
cell_2 = Cell(8, 7) | ||
|
||
result_cell = cell_1 + cell_2 | ||
print(result_cell) | ||
|
||
result_cell = cell_1 - cell_2 | ||
print(result_cell) | ||
|
||
result_cell = cell_1 * cell_2 | ||
print(result_cell) | ||
|
||
result_cell = cell_1 / cell_2 | ||
print(result_cell) | ||
|
||
cell_1.make_order() | ||
cell_2.make_order() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выполнено |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
выполнено