You are viewing a single comment's thread from:

RE: Payment for Coding done

Function to recommend the best time to book a flight to a destination

def recommend_booking_time(flight_data, destination):
# Filter flight data for the specified destination
destination_data = [flight for flight in flight_data if flight["destination"] == destination]

if not destination_data:
    return "No data available for this destination."
Sort:  

Sort flight data by date

sorted_data = sorted(destination_data, key=lambda x: x["date"])

Calculate the average price for each month

monthly_average_prices = {}
for flight in sorted_data:
    year_month = flight["date"][:7]  # Extract year and month (e.g., "2023-09")
    if year_month not in monthly_average_prices:
        monthly_average_prices[year_month] = {"total_price": 0, "count": 0}
    monthly_average_prices[year_month]["total_price"] += flight["price"]
    monthly_average_prices[year_month]["count"] += 1

Calculate the average price for each month

for month, data in monthly_average_prices.items():
    data["average_price"] = data["total_price"] / data["count"]

Find the month with the lowest average price

best_month = min(monthly_average_prices, key=lambda x: monthly_average_prices[x]["average_price"])
best_month_avg_price = monthly_average_prices[best_month]["average_price"]

return f"The best time to book a flight to {destination} is in {best_month} with an average price of ${best_month_avg_price:.2f}."

Coin Marketplace

STEEM 0.20
TRX 0.19
JST 0.034
BTC 91046.79
ETH 3087.94
USDT 1.00
SBD 2.87