From af9bb96f4c47d302b89e926601d54da01d6ec213 Mon Sep 17 00:00:00 2001 From: oh Date: Fri, 17 Jan 2025 00:09:35 +0200 Subject: [PATCH 1/5] Solution --- app/main.py | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index b2d096bda..db93ed8b8 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,40 @@ class Car: - # write your code here - pass + def __init__(self, comfort_class: int, + clean_mark: int, brand: str) -> None: + self.comfort_class = comfort_class + self.clean_mark = clean_mark + self.brand = brand class CarWashStation: - # write your code here - pass + def __init__(self, distance_from_city_center: float, + clean_power: int, average_rating: float, + count_of_ratings: int) -> None: + self.distance_from_city_center = distance_from_city_center + self.clean_power = clean_power + self.average_rating = average_rating + self.count_of_ratings = count_of_ratings + + def serve_cars(self, cars: list) -> float: + total_income = 0 + for car in cars: + if car.clean_mark < self.clean_power: + total_income += self.calculate_washing_price(car) + self.wash_single_car(car) + return round(total_income, 1) + + def calculate_washing_price(self, car: Car) -> float: + return round(car.comfort_class + * (self.clean_power - car.clean_mark) + * self.average_rating + / self.distance_from_city_center + , 1) + + def wash_single_car(self, car: Car) -> None: + car.clean_mark = self.clean_power + + def rate_service(self, rate: int) -> None: + total_ratings_sum = self.average_rating * self.count_of_ratings + self.count_of_ratings += 1 + self.average_rating = round((total_ratings_sum + rate) + / self.count_of_ratings, 1) From 30e86e6afdb74ce499188d36c1fc66a76d62a36d Mon Sep 17 00:00:00 2001 From: oh Date: Fri, 17 Jan 2025 00:16:28 +0200 Subject: [PATCH 2/5] Handle a potential division by zero runtime error --- app/main.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/main.py b/app/main.py index db93ed8b8..5c3b24930 100644 --- a/app/main.py +++ b/app/main.py @@ -24,6 +24,9 @@ def serve_cars(self, cars: list) -> float: return round(total_income, 1) def calculate_washing_price(self, car: Car) -> float: + if self.distance_from_city_center == 0: + raise ValueError("Distance from city center must be greater than zero.") + return round(car.comfort_class * (self.clean_power - car.clean_mark) * self.average_rating From ae3450e741b40f17d1b09de007a376ca5f4f0e21 Mon Sep 17 00:00:00 2001 From: oh Date: Fri, 17 Jan 2025 00:19:05 +0200 Subject: [PATCH 3/5] Fix flake8 formatting --- app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 5c3b24930..8343cddf2 100644 --- a/app/main.py +++ b/app/main.py @@ -25,7 +25,9 @@ def serve_cars(self, cars: list) -> float: def calculate_washing_price(self, car: Car) -> float: if self.distance_from_city_center == 0: - raise ValueError("Distance from city center must be greater than zero.") + raise ValueError( + "Distance from city center must be greater than zero." + ) return round(car.comfort_class * (self.clean_power - car.clean_mark) From 838a44c723548e0286143ecc3f5e21c502cd974e Mon Sep 17 00:00:00 2001 From: oh Date: Fri, 17 Jan 2025 15:13:33 +0200 Subject: [PATCH 4/5] Fix another way zero division error --- app/main.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index 8343cddf2..28b1b8d0c 100644 --- a/app/main.py +++ b/app/main.py @@ -24,16 +24,16 @@ def serve_cars(self, cars: list) -> float: return round(total_income, 1) def calculate_washing_price(self, car: Car) -> float: - if self.distance_from_city_center == 0: - raise ValueError( - "Distance from city center must be greater than zero." + try: + return round( + car.comfort_class + * (self.clean_power - car.clean_mark) + * self.average_rating + / self.distance_from_city_center, + 1 ) - - return round(car.comfort_class - * (self.clean_power - car.clean_mark) - * self.average_rating - / self.distance_from_city_center - , 1) + except ZeroDivisionError: + return 0 def wash_single_car(self, car: Car) -> None: car.clean_mark = self.clean_power From 9d9260d1ef5c1d809112f20f036a9fe4dd121935 Mon Sep 17 00:00:00 2001 From: oh Date: Fri, 17 Jan 2025 15:19:30 +0200 Subject: [PATCH 5/5] Revert "Fix another way zero division error" This reverts commit 838a44c723548e0286143ecc3f5e21c502cd974e. --- app/main.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index 28b1b8d0c..8343cddf2 100644 --- a/app/main.py +++ b/app/main.py @@ -24,16 +24,16 @@ def serve_cars(self, cars: list) -> float: return round(total_income, 1) def calculate_washing_price(self, car: Car) -> float: - try: - return round( - car.comfort_class - * (self.clean_power - car.clean_mark) - * self.average_rating - / self.distance_from_city_center, - 1 + if self.distance_from_city_center == 0: + raise ValueError( + "Distance from city center must be greater than zero." ) - except ZeroDivisionError: - return 0 + + return round(car.comfort_class + * (self.clean_power - car.clean_mark) + * self.average_rating + / self.distance_from_city_center + , 1) def wash_single_car(self, car: Car) -> None: car.clean_mark = self.clean_power