Write a function that computes the time one must leave in or
\"Write a function that computes the time one must leave in order to reach a
certain destination by a designated time. You need to deal only with arrivals
occurring later in the same day as the departure. Function inputs include the
arrival time as an integer on a 24-hour clock (8:30 p.m. = 2030), the distance to
the destination in kilometers, and the speed you plan to average in km/hr. The
function result should be the required departure time (rounded to the nearest
minute) as an integer on a 24-hour clock. Also, write a driver program to test
your function.\"
Solution
def calculate_departure_time(arrival_time, distance_in_km, average_speed_in_km_per_hour):
\"\"\"
returns deparature time from the 3 parameters
\"\"\"
time_taken_in_hour = float(distance_in_km)/average_speed_in_km_per_hour
time_taken_in_min = int(time_taken_in_hour*60)
no_hours = time_taken_in_min/60
no_mins = time_taken_in_min % 60
arrival_hour = arrival_time/100
arrival_min = arrival_time%100
print \"time taken to travel is \", no_hours,\" hours \", no_mins, \" mins \"
if no_mins < arrival_min:
deparature_min = arrival_min - no_mins
deparature_hour = arrival_hour - no_hours
else:
deparature_min = arrival_min - no_mins + 60
deparature_hour = arrival_hour - no_hours - 1
deparature_time = deparature_hour*100 + deparature_min
return deparature_time
if __name__ == \'__main__\':
print calculate_departure_time(2030, 100, 100)
print calculate_departure_time(2030, 100, 30)
print calculate_departure_time(2010, 100, 30)
