Consider you are standing at position 'A' and your destination is position 'B' (in a straight line). You car's fuel tank could hold fuel for 'L' km/miles. There are 'n' gas stations in the way of 'A' and 'B' including 'A' as the first gas station and 'B' as last gas station. You will be given with capacity of gas tank 'L', a list of gas station 'x' and the length of the list 'n'. Also 'A' (the starting position) is first index of 'n' and 'B' (the destination) is the last position of 'n'. You have to answer the minimum number of refill you have to do before getting to 'B' (your tank is full at 'A'). Every number in list 'x' ie. x[i] is a gas station distance from 'A'.
So I wrote this code.....
totalrefill, currentrefill = 0, 0
while currentrefill<=n:
lastrefill = currentrefill
while (currentrefill<n) and (x[currentrefill+1]-x[lastrefill]<=L):
currentrefill += 1
if currentrefill==lastrefill:
return "IMPOSSIBLE"
if currentrefill<=n:
totalrefill+=1
return totalrefill
x = [0, 2, 3.5, 5, 7, 8.5, 9.5]
L = 4
n = len(x)
print(min_refuels(x,n,L))
But I don't understand why it is showing list index out of range. If anyone gets it and answers it then thanks a lot.