IN Python please A Shortest Distance The shortest distance b

IN Python please!!

A. Shortest Distance

The shortest distance between 2 points ( x 1 , y 1 ), ( x 2 , y 2 ) on a Carte sian plane is given by Pythagoras:

sqrt((x2-x1)**2 + (y2-y1)**2)

Write a pure function named shortestDist that will take a list of points as its only argument and return the shortest distance between any two points in the list. Each point in the list is represented by a list of two elements:

[ [ x 1 ,y 1 ], [ x 2 ,y 2 ], ... , [ x n ,y n ] ]

Note that this will require an \"all - pairs\" comparison. Avoid comparing a point with itself!

Include a function named unitTest that calls the shortestDist function and displays the result for the following ( at a minimum):

[[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]]

Solution

def dis(a,b):
   x1 = a[0]
   y1 = a[1]
   x2 = b[0]
   y2 = b[1]
   tmp = ((x2-x1)**2 + (y2-y1)**2)
   return tmp**(0.5)
def shortestDist(lis):
   l = len(lis)
   ans = dis(lis[0],lis[1])
   p = 0
   q = 1
   for i in range(0,l):
       for j in range(i+1,l):
           tmp = dis(lis[i],lis[j])
           if(tmp<ans):
               ans = tmp
               p=i
               q=j
   #print(lis[p])
   #print(lis[q])
   return ans
def unitTest():
   a = [[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]]
   print(shortestDist(a))
unitTest()

IN Python please!! A. Shortest Distance The shortest distance between 2 points ( x 1 , y 1 ), ( x 2 , y 2 ) on a Carte sian plane is given by Pythagoras: sqrt((

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site