Closest Points

Carvia Tech | June 21, 2019 | 1 min read | 17 views

In this problem, you are given a list of x, y values that represent points in an x, y coordinate system. Process the list of x, y coordinates to determine which two are the closest. In each test case, there will be only one pair of points that are the "closest". The distance (d) between two points (x1, y1) and (x2, y2) is given by the following formula:


Consider the three points listed below.

24 12 25 54 33 13

The three points are (24, 12), (25, 54), and (33, 13). For output you will need to identify the two closest points and the distance between them. For the three points above the results are:

Closest 2 points are (24, 12) and (33, 13). The distance is 9.055385138137417

def calculate_dist_btwn_points(p1,p2):
    dist = ((p2[0]-p1[0])**2 + (p2[1]-p2[1])**2)**0.5
    return dist

def get_closest_pts_dist(list_of_num):
    tp = []
    for i,j in zip(list_of_num[::2],list_of_num[1:][::2]):
    for i,j in enumerate(tp):
        # print(j)
    return tps[dists.index(max(dists))], max(dists)

print(get_closest_pts_dist([24, 12, 25, 54, 33, 13]))

([(25, 54), (33, 13)], 8.0)

Top articles in this category:
  1. Python coding challenges for interviews
  2. Top 100 interview questions on Data Science & Machine Learning
  3. Flask Interview Questions
  4. Google Data Scientist interview questions with answers
  5. When using Gaussian mixture model, how do you know it is applicable
  6. How to design a customer satisfaction survey
  7. What is the difference between K-mean and EM? How to decide k

Find more on this topic:
Machine Learning image
Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info