# Closest Points

Carvia Tech | June 21, 2019 | 1 min read | 14 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]):
tp.append((i,j))
dists=[]
tps=[]
for i,j in enumerate(tp):
# print(j)
try:
dists.append(calculate_dist_btwn_points(tp[i],tp[i+1]))
tps.append([tp[i],tp[i+1]])
except:
continue
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:

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

###### Find more on this topic:

##### 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:

##### Similar Posts

- Installing PySpark with Jupyter notebook on Ubuntu 18.04 LTS
- Send email with attachment in Python
- Send rich text multimedia email in Python
- Blueprints in Flask API Development
- Singleton Design Pattern in Python
- SVM after LSTM deep learning model for text classification
- RuntimeError: get_session is not available when using TensorFlow 2.0
- Deploying Keras Model in Production with TensorFlow 2.0
- Python coding challenges for interviews
- Python Flask Interview Questions

Enter your email address to subscribe to this blog and receive notifications of new posts by email.