A frog wants to cross a river.
There are 3 stones in the river she can jump to.
She wants to choose among all possible paths the one that leads to the smallest longest jump.
Ie. each of the possible paths will have one jump that is the longest. She needs to find the path where this longest jump is smallest.
The 2 shores are 10 apart and are parallel to the y axis.
Each stone position is given by a list x=[x1,x2,x3] of the x positions and y=[y1,y2,y3] of the y positions.
Return both the longest jump in this path (rounded to the closest integer) and the path itself through a list of indices in the lists x and y of the stones in the path.
Here it is my python code to find the longest jump.
How would I track the path itself?
And my code looks clumsy with 3 nested loops is there a better/more elegant way to write this code?
def longestJump(x, y):
best = 10
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
# first jump from shore to a stone
dist = x[i]
# second jump between stones
dist = max(dist, round(math.sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2)))
# third jump between stones
dist = max(dist, round(math.sqrt((x[i]-x[k])**2 + (y[i]-y[k])**2)))
dist = max(dist, round(math.sqrt((x[j]-x[k])**2 + (y[j]-y[k])**2)))
# last jump from a stone to the opposite shore
dist = max(dist, 10 - x[j])
best = min(dist, best)
return best