Learn practical skills, build real-world projects, and advance your career

Exercise - Data Analysis for Vacation Planning

You're planning a vacation, and you need to decide which city you want to visit. You have shortlisted four cities and identified the return flight cost, daily hotel cost, and weekly car rental cost. While renting a car, you need to pay for entire weeks, even if you return the car sooner.

CityReturn Flight ($)Hotel per day ($)Weekly Car Rental ($)
Paris20020200
London25030120
Dubai3701580
Mumbai4501070

Answer the following questions using the data above:

  1. If you're planning a 1-week long trip, which city should you visit to spend the least amount of money?
  2. How does the answer to the previous question change if you change the trip's duration to four days, ten days or two weeks?
  3. If your total budget for the trip is $1000, which city should you visit to maximize the duration of your trip? Which city should you visit if you want to minimize the duration?
  4. How does the answer to the previous question change if your budget is $600, $2000, or $1500?

Hint: To answer these questions, it will help to define a function cost_of_trip with relevant inputs like flight cost, hotel rate, car rental rate, and duration of the trip. You may find the math.ceil function useful for calculating the total cost of car rental.

import math

days = 7

#Calculate number of weeks to pay
if days % 7 == 0:
	weeks = days / 7
else:
	weeks = math.ceil(days / 7) #Round out to the next number

city_list = ["Paris", "London", "Dubai", "Mumbai"]

flight_cost = {"Paris":200, "London":250, "Dubai":370, "Mumbai":450}
hotel_cost = {"Paris":20, "London":30, "Dubai":15, "Mumbai":10}
car_cost = {"Paris":200, "London":120, "Dubai":80, "Mumbai":70}

costs_list = {}

#Calculate cost for each city
for city in city_list:
	cost = flight_cost.get(city) + hotel_cost.get(city) * days + car_cost.get(city) * weeks
	costs_list[city] = cost

cheapest_value = min(costs_list.values())
for city in costs_list.keys(): #Print cheapest city
	if costs_list.get(city) == cheapest_value:
		print (city)  
Paris
import math

city_list = ["Paris", "London", "Dubai", "Mumbai"]

flight_cost = {"Paris":200, "London":250, "Dubai":370, "Mumbai":450}
hotel_cost = {"Paris":20, "London":30, "Dubai":15, "Mumbai":10}
car_cost = {"Paris":200, "London":120, "Dubai":80, "Mumbai":70}

budget = 1000

max_days_per_city = {} #Dictionary to store maximum number of days per city for which cost is below the budgt

for city in city_list:
	city_day_cost = {} #Dictionary to store the cost for a determinate number of days
	
	for days in range (1, 1000, 1): 		#Calculate number of weeks to pay
		if days % 7 == 0:
			weeks = days / 7
		else:
			weeks = math.ceil(days / 7) #Round out to the next number
		
		cost = flight_cost.get(city) + hotel_cost.get(city) * days + car_cost.get(city) * weeks
		if cost < budget: #Only days for which cost is below budget
			city_day_cost[days] = cost
	
	max_days_city = max(city_day_cost.keys()) #Determinate maximum number of days for city
	max_days_per_city[city] = max_days_city

#Determinate maximum number of days of all cities
worthwhile_days = max(max_days_per_city.values()) 
for city in max_days_per_city:
	if max_days_per_city[city] == worthwhile_days:
		print(city)
Mumbai
import math

city_list = ["Paris", "London", "Dubai", "Mumbai"]

flight_cost = {"Paris":200, "London":250, "Dubai":370, "Mumbai":450}
hotel_cost = {"Paris":20, "London":30, "Dubai":15, "Mumbai":10}
car_cost = {"Paris":200, "London":120, "Dubai":80, "Mumbai":70}

budget = 600

max_days_per_city = {} #Dictionary to store maximum number of days per city for which cost is below the budgt

for city in city_list:
	city_day_cost = {} #Dictionary to store the cost for a determinate number of days
	
	for days in range (1, 1000, 1): 		#Calculate number of weeks to pay
		if days % 7 == 0:
			weeks = days / 7
		else:
			weeks = math.ceil(days / 7) #Round out to the next number
		
		cost = flight_cost.get(city) + hotel_cost.get(city) * days + car_cost.get(city) * weeks
		if cost < budget: #Only days for which cost is below budget
			city_day_cost[days] = cost
	max_days_city = max(city_day_cost.keys()) #Determinate maximum number of days for city
	max_days_per_city[city] = max_days_city

#Determinate minmum number of days of all cities
worthwhile_days = min(max_days_per_city.values()) 
for city in max_days_per_city:
	if max_days_per_city[city] == worthwhile_days:
		print(city)
Paris London Dubai Mumbai
!pip install jovian --upgrade --quiet