Write a function called
dist
that takes in two points (so two lists of two elements each), and computes the distance between them. Make sure this works on the example below before proceeding to the next step.
Usedist
in a nested loop insideshortestDist
to compare each element of the list of points with every element in the list after it. So, basically, find the shortest distance between points in a list.
Here's what I have so far:
sample= [[45, -99], [24, 83], [-48, -68], [-97, 99], [-8, -77], [-2, 50], [44, 41], [-48, -58], [-1, 53], [14, 86], [31, 94], [12, -91], [33, 50], [82, 72], [83, -90], [10, 78], [7, -22], [90, -88], [-21, 5], [6, 23]]
def dist(p0, p1):
return (((p0[0] - p1[0])**2) + ((p0[1] - p1[1])**2))**.5
def shortestDist(sample):
distances = []
for i in range(len(sample)-1):
for j in range(i+1, len(sample)):
distances += [dist(sample[i],sample[j])]
return(min(distances))
That finds the distance alright between two points. I just need some help figuring out how to start writing shortestDist
to compare all points and keep track of the shortest distance. UPDATE: The error was resolved and I'm good to go now. Thank you to everyone for their assistance!