Language: Python
Topic: Dictionaries
Function name: catch_flight
Parameters: dictionary, tuple containing two strings
Returns: dictionary
Description: You’ve been stuck in NYC for around 8 months all by
yourself because you haven’t been able to find a good time to fly
home. You’re willing to go to any city but want to see how many
flights to each location fit your budget.
You’re given a dictionary that has city names (strings) as the keys
and a list of prices (list) and a tuple containing the range of
prices (strings) that you are looking for (inclusive). Create a
dictionary that has each city name (string) as a key with a value
of the number of flight prices (int) that fall within your price
range.
>>> cities = {'Atlanta': ['$7.31', '$8.46', '$24.03',
'$12.00', '$8.30'], 'Orlando': ['$6.36', '$9.43', '$15.04',
'$19.00'], 'Phoenix': ['$1.01', '$3.18', '$4.30', '$18.01']}
>>> prices = ('$8.30', '$17.00')
>>> print(catch_flight(cities, prices))
{'Atlanta': 3, 'Orlando': 2, 'Phoenix': 0}
OUTPUT :
CODE :
def catch_flight(cities,prices):
#Convert range to float
r =
(float(prices[0][1:]),float(prices[1][1:]))
#Initialize dictionary for ans
ans = {}
for city in cities:
if city not in
ans:
ans[city] = 0
#Compare prices and add
to ans
for price in
cities[city]:
p = float(price[1:])
if p>=r[0] and p<=r[1]:
ans[city]+=1
return ans
Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description:...