Advertisement
FiddleComputers

Closest points correction

Mar 18th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #################################################################
  2. #   The goal of this exercize is to write an algorithm that ask #
  3. #   the user to enter how many points he wants to compare. Then,#
  4. #   he must enter their coordinates. The algorithm determines   #
  5. #   what are the two closest points.                            #
  6. #   Here is a possible correction.                              #
  7. #################################################################
  8.  
  9. ptsNumber = int(raw_input("How many points would like to declare? "))
  10. points = []
  11. minDist = float(10**10)
  12. minPoses = [0, 0]
  13. for i in range(0,ptsNumber):
  14.     points.append([])
  15.     points[i].append(float(raw_input("X"+str(i)+" = ")))
  16.     points[i].append(float(raw_input("Y"+str(i)+" = ")))
  17. for i in range(0, len(points)):
  18.     for j in range(0, len(points)):
  19.         if i != j and ((points[i][0] - points[j][0]) ** 2) + ((points[i][1] - points[j][1]) ** 2) ** 0.5 < minDist:
  20.             minDist = ((points[i][0] - points[j][0]) ** 2) + ((points[i][1] - points[j][1]) ** 2) ** 0.5
  21.             minPoses = [i, j]
  22. print("Points ("+str(points[minPoses[0]][0])+";"+str(points[minPoses[0]][1])+") and ("+str(points[minPoses[1]][0])+";"+str(points[minPoses[1]][1])+") are the closest with "+str(minDist)+" distance.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement