# Closest Points

Carvia Tech | June 21, 2019 | 1 min read | 16 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:

- Top 100 interview questions on Data Science & Machine Learning
- Google Data Scientist interview questions with answers
- Python coding challenges for interviews
- Python Flask Interview Questions
- 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

- Configure Logging in gunicorn based application in docker container
- Connect to Cassandra with Python 3.x and get Pandas Dataframe
- Connect to MySQL with Python 3.x and get Pandas Dataframe
- Connect to Postgresql with Python 3.x and get Pandas Dataframe
- Python - Get Google Analytics Data
- Installing PySpark with Jupyter notebook on Ubuntu 18.04 LTS
- Python send GMAIL with attachment
- Send rich text multimedia email in Python
- Blueprints in Flask API Development
- Singleton Design Pattern in Python