-
Notifications
You must be signed in to change notification settings - Fork 0
/
032TheNile.py
49 lines (41 loc) · 1.45 KB
/
032TheNile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from math import sin, cos, atan2, sqrt
def get_distance(from_lat, from_long, to_lat, to_long):
dlon = to_long - from_long
dlat = from_lat - to_lat
a = (sin(dlat/2)) ** 2 + cos(from_lat) * cos(to_lat) * (sin(dlon/2)) ** 2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = a * c
return distance
SHIPPING_PRICES = {
'Ground': 1,
'Priority': 1.6,
'Overnight': 2.3,
}
def format_price(price):
return "${0:.2f}".format(price)
def calculate_shipping_cost(from_coords, to_coords, shipping_type = "Overnight"):
from_lat, from_long = from_coords
to_lat, to_long = to_coords
distance = get_distance(from_lat, from_long, to_lat, to_long)
shipping_rate = SHIPPING_PRICES[shipping_type]
price = distance * shipping_rate
return format_price(price)
def calculate_driver_cost(distance, *drivers):
cheapest_driver = None
cheapest_driver_price = None
for driver in drivers:
driver_time = driver.speed * distance
price_for_driver = driver.salary * driver_time
if cheapest_driver is None:
cheapest_driver = driver
cheapest_driver_price = price_for_driver
elif price_for_driver < cheapest_driver_price:
cheapest_driver = driver
cheapest_driver_price = price_for_driver
return cheapest_driver_price, cheapest_driver
def calculate_money_made(**trips):
total_money_made = 0
for trip_id, trip in trips.items():
trip_revenue = trip.cost - trip.driver.cost
total_money_made += trip_revenue
return total_money_made