# Exercise - Data Analysis for Vacation Planning

what are the answers to these questions with their respective values
???

I tired to slove out this exercise in this fashion

``````def cost_trip(flight_rate,hotel_rate,car_rentel,duration_in_days):
total_hotel_cost = hotel_rate/duration_in_days;
total_car_rentel = math.ceil((duration_in_days/7))*car_rentel
total_trip_cost=total_hotel_cost+total_car_rentel+flight_rate;
return math.ceil(total_trip_cost);

mumbai=cost_trip(flight_rate=450,hotel_rate=10,car_rentel=70,duration_in_days=14);
mumbai
591``````

Does anyone know if this is correct? I am using python but it doesnt execute anything.

# Vacation planning exercise.

cost_flight_Paris = 200
cost_hotel_Paris = 20 * 7
cost_car_Paris = 200
cost_flight_London = 250
cost_hotel_London = 30 * 7
cost_car_London = 120
cost_flight_Dubai = 370
cost_hotel_Dubai = 15 * 7
cost_car_Dubai = 80
cost_flight_Mumbai = 450
cost_hotel_Mumbai = 10 * 7
cost_car_Mumbai = 70

Cost_Paris = cost_flight_Paris + cost_hotel_Paris + cost_car_Paris
Cost_London = cost_flight_London + cost_hotel_London + cost_car_London
Cost_Dubai = cost_flight_Dubai + cost_hotel_Dubai + cost_car_Dubai
Cost_Mumbai = cost_flight_Mumbai + cost_hotel_Mumbai + cost_car_Mumbai

def one_week_long_trip():
if Cost_Paris < Cost_London <= Cost_Dubai <= Cost_Mumbai:
return print(“The cheapest 1-week long trip would be Paris with a cost of \$ {}” .format(Cost_Paris)),
elif Cost_London < Cost_Paris <= Cost_Dubai <= Cost_Mumbai:
return print(“The cheapest 1-week long trip would be London with a cost of \$ {}” .format(Cost_London)),
elif Cost_Dubai < Cost_London <= Cost_Paris <= Cost_Mumbai:
return print(“The cheapest 1-week long trip would be Dubai with a cost of \$ {}”.format(Cost_Dubai)),
elif Cost_Mumbai < Cost_Dubai <= Cost_London <= Cost_Paris:
return print(“The cheapest 1-week long trip would be Mumbai with a cost of \$ {}”.format(Cost_Mumbai)),

one_week_long_trip()

This is my solution to the first 2 questions, I am having a tough time collecting my thoughts on the budgeting ones

The Cheapest City is Paris, and total price to visit that city is \$ 540

The Longest City is London, and days to visit that city is 2.5

Now i know its a long code and can be reduced i just dont yet know how-

import math
dict={‘Paris’:[200,20,200],
‘London’:[250,30,120],
‘Dubai’:[370,15,80],
‘Mumbai’:[450,10,70]}

def cost_of_vacation_in_paris(duration,flight_cost=dict[‘Paris’][0]
,hotel_rate=dict[‘Paris’][1]
,car_rate=dict[‘Paris’][2]
):
hotel_cost=hotel_rateduration
duration=math.ceil(duration/7)
car_cost=car_rate
duration
total_cost=flight_cost+hotel_cost+car_cost
return math.ceil(total_cost)

def cost_of_vacation_in_london(duration,flight_cost=dict[‘London’][0]
,hotel_rate=dict[‘London’][1]
,car_rate=dict[‘London’][2]
):
hotel_cost=hotel_rateduration
duration=math.ceil(duration/7)
car_cost=car_rate
duration
total_cost=flight_cost+hotel_cost+car_cost
return math.ceil(total_cost)

def cost_of_vacation_in_dubai(duration,flight_cost=dict[‘Dubai’][0]
,hotel_rate=dict[‘Dubai’][1]
,car_rate=dict[‘Dubai’][2]
):
hotel_cost=hotel_rateduration
duration=math.ceil(duration/7)
car_cost=car_rate
duration
total_cost=flight_cost+hotel_cost+car_cost
return math.ceil(total_cost)

def cost_of_vacation_in_mumbai(duration,flight_cost=dict[‘Mumbai’][0]
,hotel_rate=dict[‘Mumbai’][1]
,car_rate=dict[‘Mumbai’][2]
):
hotel_cost=hotel_rateduration
duration=math.ceil(duration/7)
car_cost=car_rate
duration
total_cost=flight_cost+hotel_cost+car_cost
return math.ceil(total_cost)

#for last 2 questions
def budget_given_paris(budget):
budget=budget-dict[‘Paris’][0]
i=0
while (dict[‘Paris’][1]*i+(math.ceil(i/7))*dict[‘Paris’][2])<=budget:
i=i+1
return i-1
def budget_given_london(budget):
budget=budget-dict[‘London’][0]
i=0
while (dict[‘London’][1]*i+(math.ceil(i/7))*dict[‘London’][2])<=budget:
i=i+1
return i-1
def budget_given_dubai(budget):
budget=budget-dict[‘Dubai’][0]
i=0
while (dict[‘Dubai’][1]*i+(math.ceil(i/7))*dict[‘Dubai’][2])<=budget:
i=i+1
return i-1
def budget_given_mumbai(budget):
budget=budget-dict[‘Mumbai’][0]
i=0
while (dict[‘Mumbai’][1]*i+(math.ceil(i/7))*dict[‘Mumbai’][2])<=budget:
i=i+1
return i-1

The loop for all cities is only printing Mumbai with a total cost of 540. How can set it up to print ALL cities?

Please check the code again, you might be trying to print the statement separately. That’s why it must be showing the result for the first city only. Use the print statement inside the for loop.

Sorry, I posted the whole solution. I have to remove it because that’s not ethical. Please try on your own. Feel free to contact me if you need further help.

Hey!
Was anyone able to calculate the duration for the different budget?

Hey @subhasray1996, welcome to the forum.
Here’s one solution using a 2-D List

``````import math

Paris=[200,20,200,'Paris']
London = [250,30,120,'London']
Dubai = [370,15,80,'Dubai']
Mumbai = [450,10,70,'Mumbai']
Cities = [Paris,London,Dubai,Mumbai]

def cost_of_trip(flight,hotel_cost,car_rent,num_of_days=0):
return flight*2+hotel_cost*num_of_days+car_rent*math.ceil(num_of_days/7)

def days_to_visit(days):
min_cost_di = {}
for city in Cities:
cost=cost_of_trip(city[0],city[1],city[2],days)
if min_cost_di.get(cost) is None:
min_cost_di[cost]=[]
min_cost_di[cost].append(city[3])
min_cost = min(list(min_cost_di.keys()))
return min_cost_di[min_cost],min_cost

# less_days=False if you want the maximum number of days within budget
# less_days=True if you want the minimum days within budget
def given_budget(budget,less_days=False):
days=1
cost=0
while cost<budget:
cost_di={}
for city in Cities:
cost = cost_of_trip(city[0],city[1],city[2],days)
if cost_di.get(cost):
cost_di[cost].append(city[3])
cost_di[cost] = [city[3]]
days+=1
if less_days:
cost=max(list(cost_di.keys()))
else:
cost=min(list(cost_di.keys()))
return *cost_di[cost], days
``````

You can also check this made using Classes in Python:

1 Like

Below is the snippet code for the Vacation Planning Exercise. Kindly correct me if the logic is wrong.

# Planning a Vacation and estimate amount for trip

def cost_of_trip(noofdays, flight_fee, hotel_per_day, car_rent):
noofweeks = noofdays / 7
total_cost = (car_rent * noofweeks) + flight_fee + ((noofweeks * 7) * hotel_per_day)
return math.ceil(total_cost)

def city_cost(noofweeks):
citytrip_cost = {}
citytrip_cost[“Paris”] = cost_of_trip(noofweeks,flight_fee=200, hotel_per_day= 20, car_rent= 200)
citytrip_cost[“London”] = cost_of_trip(noofweeks,flight_fee=250, hotel_per_day= 30, car_rent= 120)
citytrip_cost[“Dubai”] = cost_of_trip(noofweeks,flight_fee=370, hotel_per_day= 15, car_rent= 80)
citytrip_cost[“Mumbai”] = cost_of_trip(noofweeks,flight_fee=450,hotel_per_day= 10, car_rent= 70)
return citytrip_cost

def duration(budget,flight_fee,car_rent,hotel_per_day):
noofweeks =math.ceil((budget - flight_fee) / (car_rent + (7 * hotel_per_day)))
return math.ceil(noofweeks/7)

def city_duration(budget):
citytrip_duration ={}
citytrip_duration[“Paris”] = duration(budget,flight_fee=200, hotel_per_day= 20, car_rent= 200)
citytrip_duration[“London”] = duration(budget,flight_fee=250, hotel_per_day= 30, car_rent= 120)
citytrip_duration[“Dubai”] = duration(budget,flight_fee=370, hotel_per_day= 15, car_rent= 80)
citytrip_duration[“Mumbai”] = duration(budget,flight_fee=450,hotel_per_day= 10, car_rent= 70)
return citytrip_duration

no_of_days = [4,10,14]
for i in no_of_days:
all_cost = city_cost(i)
print(f"City to spend least amount: {min(all_cost,key=all_cost.get)} for duration of {i} days")
available_budget = [600,2000,1500]
for i in available_budget:
durationoftrip = city_duration(i)
print(f"City with maximum duration with {i} budget is {max(durationoftrip,key=durationoftrip.get)}")

hi all !

i see ‘While renting a car, you need to pay for entire weeks, even if you return the car sooner’. Can i ask : it mean if i rent a car < 7days, i will pay the cost on the table, if i rent a car > 7 day (8 , 9 , 10,…) maybe i will *2 the cost ?? is my opion excaclty ?

This is how I solved exercise 1 and 2. I’m still trying to figure it out how to do the ones with budgets

``````# Exercise 1

paris = [200, 20, 200]
london = [250, 30, 120]
dubai = [370, 15, 80]
mumbai = [450, 10, 70]

def cost_of_trip(flight, hotel, car):
cost = flight + hotel * 7 + car
return cost

print(cost_of_trip(*paris), ': Paris')  # the '*' unpacks the list to arguments for the function
print(cost_of_trip(*london), ': London')
print(cost_of_trip(*dubai), ': Dubai')
print(cost_of_trip(*mumbai), ': Mumbai')

# You should visit Paris to spend the least amount of money in a week
``````
``````# Exercise 2

import math

def cost_of_trip(flight, hotel, car, days):
'''calculates the cost of the trip taking
into account the number of days'''

car_cost = car * math.ceil(days/7)
cost = flight + hotel * days + car_cost
return cost
``````
1 Like

Here is my solution for the exercise. I’m posting it here as it could help give a hint to others that are stuck on something, maybe as I’ve been on the logic of the nesting of the last two questions.

``````import math
paris = [200, 20, 200, 'Paris']
london = [250, 30, 120, 'London']
dubai = [370, 15, 80, 'Dubai']
mumbai = [450, 10, 70, 'Mumbai']
Cities = [paris, london, dubai, mumbai]

def tripcost(flight, hotel, car, duration):
""" Insert return flight cost, daily hotel cost, car rental weekly rate, duration of the trip in days """
return flight + (hotel * duration) + (car * math.ceil(duration / 7))

def daystovisit(days):
citiescost = {}
for city in Cities:
cost = tripcost(city[0], city[1], city[2], days)
citiescost[city[-1]] = cost
print('The cheapest {} days\' trip destination is {} and it costs \${}.'.format(days, min(citiescost, key=citiescost.get), citiescost.get(min(citiescost, key=citiescost.get))))

def optimaltrip(budget, minimize=False):
days = 1
cost = 0
citytrip = {}
optimals = {}

while cost < budget:
for city in Cities:
cost = tripcost(city[0], city[1], city[2], days)
if cost <= budget:
citytrip[city[-1]] = "\${} for {} days".format(cost, days)
optimals[city[-1]] = days
days += 1

if citytrip == {}:
print("Not enough budget for a trip")
elif minimize == False:
print("To maximize the duration of the trip with a \${} budget you should go to {}, where you will stay for {} days.".format(budget, max(optimals, key=optimals.get), optimals.get(max(optimals, key=optimals.get))))
else:
print("To minimize the duration of the trip with a \${} budget you should go to {}, where you will stay for {} days.".format(budget, min(optimals, key=optimals.get), optimals.get(min(optimals, key=optimals.get))))``````

def max_min_budget(budget,min_value = False):
value = []
res = []
res2 = []
for city in Cities:
amount = budget - city[0]
value = amount / ((city[1]7) + (city[2]))
res.append((value,amount,city[1],city[2],city[3]))
for x in res:
a = math.floor(x[0])
remaining_amount = x[1] - (a
((x[2]7) + (x[3])))
if remaining_amount > x[3]:
k = remaining_amount - x[3]
else:
k = 0
if k != 0:
remaining_day = (a
7) + (k//x[2])
else:
remaining_day = a*7

``````    res2.append((remaining_day,x[4]))
if min_value:
return min(res2)
else:
return max(res2)
``````

is it right way?