From 43cf6c9f912a6f9f28e1719092ea557d7a5bbc3b Mon Sep 17 00:00:00 2001 From: revotuning Date: Sun, 3 Nov 2024 10:41:46 +0300 Subject: [PATCH] 1 lesson tasks is done --- 1lession/1.1_PointXY.py | 28 +++++++++++++++++++ 1lession/1.2_Car.py | 62 +++++++++++++++++++++++++++++++++++++++++ 1lession/1.3_Rect.py | 34 ++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 1lession/1.1_PointXY.py create mode 100644 1lession/1.2_Car.py create mode 100644 1lession/1.3_Rect.py diff --git a/1lession/1.1_PointXY.py b/1lession/1.1_PointXY.py new file mode 100644 index 0000000..1a2d032 --- /dev/null +++ b/1lession/1.1_PointXY.py @@ -0,0 +1,28 @@ +from math import sqrt + + +class PointXY: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + def distance_from_zero(self): + return sqrt(pow(self.x, 2) + pow(self.y, 2)) + + # Хотелось бы типизировать входной параметр p классом PointXY + # для исключения передачи некорректных данных на вход метода. + # Просьба объяснить возможно ли это и, если да, то как. + def distance_to(self, p): + return sqrt(pow((self.x - p.x), 2) + pow((self.y - p.y), 2)) + + +def main(): + p1 = PointXY(4, 3) + p2 = PointXY(9, 6) + p3 = PointXY(5, 3) + print(p1.distance_from_zero()) + print(p2.distance_to(p3)) + + +if __name__ == '__main__': + main() diff --git a/1lession/1.2_Car.py b/1lession/1.2_Car.py new file mode 100644 index 0000000..6ed731a --- /dev/null +++ b/1lession/1.2_Car.py @@ -0,0 +1,62 @@ +class Car: + def __init__(self, spec: dict): + # Валидации в данном случае подходят только для атрибутов с числовым форматом. + # Как сделать изящнее без кучи if-ов при условии что атрибут может быть строкой (например, марка, модель и тд)? + for item in spec: + if type(spec.get(item)) not in {int, float}: + raise TypeError('Параметр %s должен быть числом' % item) + if spec.get(item) < 0: + raise ValueError('Параметр %s не может быть отрицательным' % item) + if spec.get('gas') > spec.get('capacity'): + raise ValueError('Количество топлива в баке не может быть больше объема бака') + + self.gas_l = spec.get('gas') + self.capacity_l = spec.get('capacity') + self.l_per_km = spec.get('consumption') + self.odometer = spec.get('odometer') + + def fill(self, value): + if type(value) not in {int, float}: + raise TypeError('Объем заправляемого топлива должен быть числом') + if value < 0: + raise ValueError('Объем заправляемого топлива не может быть отрицательным') + result = self.gas_l + value + if result > self.capacity_l: + self.gas_l = self.capacity_l + print("Осталось лишнее топливо: %s литров" % (result - self.capacity_l)) + else: + self.gas_l = result + + def ride(self, distance): + remain = distance + while remain > 0: + self.odometer += 1 + self.gas_l = round(self.gas_l - self.l_per_km, 2) + remain -= 1 + if self.gas_l == 0: + + break + result = distance - remain + print("Проехали %s километров" % result) + + +def main(): + c1_spec = { + "gas": 30, + "capacity": 40, + "consumption": 0.1, + "odometer": 10000 + } + c1 = Car(c1_spec) + print("Исходные данные:", c1.gas_l, c1.capacity_l, c1.l_per_km, c1.odometer) + + c1.fill(5) + print("Показатели после заправки:", c1.gas_l, c1.capacity_l, c1.l_per_km, c1.odometer) + print() + + c1.ride(100) + print("Показатели после поездки:", c1.gas_l, c1.capacity_l, c1.l_per_km, c1.odometer) + + +if __name__ == '__main__': + main() diff --git a/1lession/1.3_Rect.py b/1lession/1.3_Rect.py new file mode 100644 index 0000000..e45401d --- /dev/null +++ b/1lession/1.3_Rect.py @@ -0,0 +1,34 @@ +class Rect: + def __init__(self, height: int, width: int): + if height < 0 or width < 0: + raise ValueError('Длины сторон не могут быть отрицательными') + self.height = height + self.width = width + + def area(self): + return self.height * self.width + + def perimeter(self): + return 2 * (self.height + self.width) + + def scale(self, multiplier): + self.height *= multiplier + self.width *= multiplier + + def rotate(self): + tmp = self.height + self.height = self.width + self.width = tmp + + +def main(): + r1 = Rect(10, 56) + print(r1.area()) + print(r1.perimeter()) + r1.scale(10) + r1.scale(0.1) + r1.rotate() + + +if __name__ == '__main__': + main()