-
Notifications
You must be signed in to change notification settings - Fork 701
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
Solution #649
Open
AndrewChugreev
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
AndrewChugreev:develop
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
Solution #649
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,7 @@ | ||
class Car: | ||
def __init__(self, brand: str, fuel_consumption: float) -> None: | ||
self.brand = brand | ||
self.fuel_consumption = fuel_consumption | ||
|
||
def calculate_fuel_cost(self, distance: float, full_price: float) -> float: | ||
return (self.fuel_consumption / 100) * distance * full_price |
This file contains 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,30 @@ | ||
from app.car import Car | ||
from decimal import Decimal | ||
import math | ||
|
||
|
||
class Customer(): | ||
def __init__( | ||
self, | ||
name: str, | ||
product_cart: dict[str, int], | ||
location: list[int], | ||
money: Decimal, | ||
car: Car | ||
) -> None: | ||
self.name = name | ||
self.product_cart = product_cart | ||
self.location = location | ||
self.money = money | ||
self.car = car | ||
|
||
def distance(self, shop_location: list) -> float: | ||
return math.sqrt( | ||
(self.location[0] - shop_location[0]) ** 2 | ||
+ (self.location[1] - shop_location[1]) ** 2) | ||
|
||
def ride_home(self, spend_money: float) -> None: | ||
print(f"{self.name} rides home") | ||
money_after_shop = self.money - spend_money | ||
self.money = money_after_shop | ||
print(f"{self.name} now has {money_after_shop} dollars\n") |
This file contains 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 |
---|---|---|
@@ -1,3 +1,62 @@ | ||
def shop_trip(): | ||
# write your code here | ||
pass | ||
import json | ||
import os | ||
from app.customer import Customer | ||
from app.shop import Shop | ||
from app.car import Car | ||
|
||
|
||
def shop_trip() -> None: | ||
config_path = os.path.join(os.path.dirname(__file__), "config.json") | ||
with open(config_path, "r") as file: | ||
data = json.load(file) | ||
fuel_price, customers_, shops_ = data.values() | ||
|
||
customers = [] | ||
for custom in customers_: | ||
car = Car(**custom["car"]) | ||
customer = Customer( | ||
name=custom["name"], | ||
product_cart=custom["product_cart"], | ||
location=custom["location"], | ||
money=custom["money"], | ||
car=car | ||
) | ||
customers.append(customer) | ||
|
||
shops = [] | ||
for some_shop in shops_: | ||
shop = Shop( | ||
name=some_shop["name"], | ||
location=some_shop["location"], | ||
products=some_shop["products"] | ||
) | ||
shops.append(shop) | ||
|
||
for customer in customers: | ||
customer_money = customer.money | ||
print(f"{customer.name} has {customer_money} dollars") | ||
trip_cost_dict = {} | ||
for shop in shops: | ||
total = 0 | ||
dist_for_shop = customer.distance(shop.location) | ||
fuel_for_shop = customer.car.calculate_fuel_cost( | ||
dist_for_shop, | ||
fuel_price | ||
) | ||
products_cost = shop.products_cost(customer) | ||
total += round((products_cost + fuel_for_shop * 2), 2) | ||
trip_cost_dict[total] = shop | ||
print(f"{customer.name}'s trip to the {shop.name} costs {total}") | ||
if trip_cost_dict: | ||
if customer.money < min(trip_cost_dict): | ||
print(f"{customer.name} doesn't have enough" | ||
f" money to make a purchase in any shop") | ||
else: | ||
total = min(trip_cost_dict) | ||
current_shop = trip_cost_dict[min(trip_cost_dict)] | ||
print(f"{customer.name} rides to {current_shop.name}\n") | ||
current_shop.print_check(customer) | ||
customer.ride_home(total) | ||
|
||
|
||
shop_trip() |
This file contains 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,35 @@ | ||
from app.customer import Customer | ||
from datetime import datetime | ||
|
||
|
||
class Shop: | ||
def __init__( | ||
self, | ||
name: str, | ||
location: list[int], | ||
products: dict[str, float] | ||
) -> None: | ||
self.name = name | ||
self.location = location | ||
self.products = products | ||
|
||
def products_cost(self, customer: Customer) -> float: | ||
product_cart = customer.product_cart | ||
product_cost = 0 | ||
for product, qty in product_cart.items(): | ||
product_cost += qty * self.products.get(product) | ||
return product_cost | ||
|
||
def print_check(self, customer: Customer) -> None: | ||
current_datatime = datetime(2021, 1, 4, 12, 33, 41) | ||
current_datatime = current_datatime.strftime("%d/%m/%Y %H:%M:%S") | ||
print(f"Date: {current_datatime}") | ||
print(f"Thanks, {customer.name}, for your purchase!") | ||
print("You have bought:") | ||
for product, quantity in customer.product_cart.items(): | ||
if product in self.products: | ||
cost = self.products[product] * quantity | ||
cost_str = f"{cost:.1f}" if cost % 1 else f"{int(cost)}" | ||
print(f"{quantity} {product}s for {cost_str} dollars") | ||
print(f"Total cost is {self.products_cost(customer)} dollars") | ||
print("See you again!\n") |
Oops, something went wrong.
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.
The expression
self.products.get(product)
may returnNone
if the product is not found in the shop's inventory. This could lead to aTypeError
when multiplyingNone
byqty
. Consider providing a default value (e.g., 0) to avoid this issue.