I am trying to find the closest pair of points from a list of points in a text file. I am trying to loop through the file then append the results to an empty file, then sort that file for the shortest distance.
The input file(text) looks like this:
2 20 55 217 33 45 100 50 99 22 13 86 60 217 34 29 14 19 200 25 100 7
My challenge is creating the loop to read each pair of points in the text file. Below is the code I came up with so far:
#empty List
list2 = []
#distance calculation for 2 closest points
def closest_point(coord1,coord2):
(x1, y1) = coord1
(x2, y2) = coord2
result1 = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
return list2.append(result1)
#reading input file with pairs of coordinates
with open('c:\\closepoints.txt') as my_new_file:
contents = my_new_file.read()
list = contents.split()
list1 = zip(list[::2], list[1::2])
list1 = set(list1)
print (list1)