Details:
There are two cities, a, b. and two arrays representing flight departure time.
Flight from a to b is represented using
array a2b, and flight from b to a is represented usingarray b2a.Flight from a to b and b to a both take
100 minutes.Need to take "n" roundtripsbetween the two cities(a -> b and b -> a is ONE round trip).Assume you always take the
first flight from a to band then take theearliest available flights.
Question: Return the time you arrive back at city a.
Example:
a2b = [109, 500]
b2a = [210, 600]
n = 2(roundtrips we need to take)
Explanation:
- Take 109 from a to b, arrive at b at 109 + 100 = 209
- Take 210 from b to a, arrive at 210 + 100 = 310.
- Take 500 from a to b again.
- Arrive at b at 500 + 100 = 600, then take 600 flight back
- Arrive at a at 600 +100 = 700.
My Code:
def flight_time(a2b, b2a, n):
a2b.sort()
b2a.sort()
trips = 0
a_arrival_time = 0
i = j = 0
while trips <= n:
b_arrival_time = a2b[i] + 100
i += 1
while j < len(b2a):
if (b2a[j] - b_arrival_time) < 0:
j += 1
continue
else:
a_arrival_time = b2a[j] + 100
j += 1
trips += 1
break
return a_arrival_time
flight_time(a2b, b2a, n)
Error IndexError: list index out of range
I also get this error when n becomes large such as n=6.
It looks like the code is not handling all the edge cases.