-
Notifications
You must be signed in to change notification settings - Fork 696
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
implemented "Shop trip" behaviour #600
base: master
Are you sure you want to change the base?
Conversation
app/main.py
Outdated
|
||
|
||
def init_shops(data: list) -> list[Shop]: | ||
result_list = [] |
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.
you can use list comprehension here
app/main.py
Outdated
def shop_trip() -> None: | ||
data = get_data_from_json() | ||
fuel_price = data["FUEL_PRICE"] | ||
customers = init_customers(data["customers"]) |
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.
you can optimize your code so you don't have to iterate over the same data twice. You can basically create customer and do all the trip related actions in one loop
app/customer.py
Outdated
|
||
def calculate_trip_cost(self, shop: Shop, fuel_price: float) -> float: | ||
distance_to_shop_and_home = sqrt( | ||
(shop.location[0] - self.location[0]) ** 2 |
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.
You could create class Location with method that calculates distance between two points to better structure your code
app/customer.py
Outdated
(shop.location[0] - self.location[0]) ** 2 | ||
+ (shop.location[1] - self.location[1]) ** 2) | ||
|
||
fuel_cost_to_shop_and_home = (distance_to_shop_and_home |
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.
this could be a Car method
app/customer.py
Outdated
* (self.car.fuel_consumption / 100) | ||
* fuel_price) | ||
|
||
product_cost_total = sum(shop.products[key] * value |
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.
This could be a method defined in Shop
app/main.py
Outdated
f" money to make a purchase in any shop") | ||
return | ||
|
||
print(f"{customer.name} rides to {current_shop.name}") |
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.
you can put all this prints as a method in your Customer class - this way you will offload a bunch of code from your main function and make it cleaner
app/customer.py
Outdated
def buy_products(self, current_shop: Shop) -> float: | ||
total_cost = 0 | ||
for key, value in self.product_cart.items(): | ||
for _ in range(3): |
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.
why do you need two for loops here?
app/customer.py
Outdated
for key, value in self.product_cart.items(): | ||
for _ in range(3): | ||
cost = current_shop.products[key] * value | ||
if ".0" in str(cost): |
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.
save cost
using ternary operator and then pass it to print only once like so:
product_cost = ... if something else ...
print(f"{value} {key}s for {product_cost} dollars")
…o shop, customer, car
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.
Looks good! 🌸
No description provided.