2

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)
accdias
  • 5,160
  • 3
  • 19
  • 31
Quizote
  • 29
  • 1

1 Answers1

-2

UPDATE: Based on the comments, here is a more efficient solution to this problem that prints the same output as the original answer:

with open('c:\\closepoints.txt') as my_new_file:
    pairs = [int(x) for x in my_new_file.readline().strip().split()]
    list1 = list(zip(pairs[::2], pairs[1::2]))
    print(list1)

(original answer is below)

You could also create a regex that parses all of the numbers into a map generator object. Then you can use list unpacking to expand the list and finally make a list comprehension that condenses the list into pairs, discarding the last element if necessary. This code will print [(2, 20), (55, 217), (33, 45), (100, 50), (99, 22), (13, 86), (60, 217), (34, 29), (14, 19), (200, 25), (100, 7)]:

import re

with open('c:\\closepoints.txt') as my_new_file:
    contents = my_new_file.read()
    pairs = [*map(int, re.findall(r'\d+', contents))]
    list1 = [(pairs[i],pairs[i+1]) for i in range(0,len(pairs),2)]
    print (list1)

Adapted from this regex answer.

Zenul_Abidin
  • 573
  • 8
  • 23
  • 2
    this doesn't solve the problem, this just parses the integers into a list of points. Regex is overkill, this sounds like an assignment and you can almost certainly assume the input is well formed, just `[int(x) for x in my_new_file.readline().strip().split()]` should be fine – Boris Verkhovskiy Dec 28 '19 at 06:23
  • They specifically said *My challenge is creating the loop to read each pair of points in the text file.*, they already solved the closest point problem in the question... but at least we found a solution that's easier than using regex I guess. – Zenul_Abidin Dec 28 '19 at 06:26
  • Why do `[(pairs[i],pairs[i+1]) for i in range(0,len(pairs),2)]` when you can simply do `list(zip(pairs[::2], pairs[1::2])))` or, even better, just return `zip(pairs[::2], pairs[1::2]))`? – accdias Dec 28 '19 at 10:37
  • 1
    @accdias I think `list(zip(pairs[::2], pairs[1::2])))` makes the most sense for this question. I'm going to add it to my answer. – Zenul_Abidin Dec 28 '19 at 12:58