- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find nearest point that has the same x or y coordinate using Python
Suppose we have a set of points given in an array called pts. We also have another point (x, y) which is our current location. We are defining a valid point as, a point which shares the same x-coordinate or the same y-coordinate as our current point. We have to return the index of the valid point with the smallest Manhattan distance from our current location (x, y). If there are more than one points, then return the valid point with the smallest index. (Note: the Manhattan distance between two points (a, b) and (p, q) is |a - p| + |b - q|.
So, if the input is like pts = [(1,2),(3,1),(3,4),(2,3),(4,4)] pt = (2,4), then the output will be 2 as there are two nearest points (3,4) and (2,3), but the index of (3,4) is smaller.
To solve this, we will follow these steps −
x, y := pt
idx := -1
smallest := infinity
for each p in pts, do
if p[0] is same as x or p[1] is same as y, then
dist := |x - p[0]| + |y - p[1]|
if dist < smallest, then
idx := index of p in pts
smallest := dist
otherwise when dist is same as smallest, then
if index of p in pts < idx, then
idx := index of p in pts
smallest := dist
return idx
Let us see the following implementation to get better understanding −
Example
def solve(pts, pt): x, y = pt idx = -1 smallest = float("inf") for p in pts: if p[0] == x or p[1] == y: dist = abs(x - p[0]) + abs(y - p[1]) if dist < smallest: idx = pts.index(p) smallest = dist elif dist == smallest: if pts.index(p) < idx: idx = pts.index(p) smallest = dist return idx pts = [(1,2),(3,1),(3,4),(2,3),(4,4)] pt = (2,4) print(solve(pts, pt))
Input
[(1,2),(3,1),(3,4),(2,3),(4,4)], (2,4)
Output
2
- Related Articles
- Program to find value of find(x, y) is even or odd in Python
- Find a relation between x and y such that point$ (x, y)$ is equidistant from the point (7, 1) and (3, 5)
- Program to find nearest time by reusing same digits of given time in python
- Program to find k-th largest XOR coordinate value in Python
- C++ program to find out the number of coordinate pairs that can be made
- Find a relation between $x$ and $y$ such that the point $(x, y)$ is equidistant from the points $(3, 6)$ and $(-3, 4)$.
- Python Program to find the Next Nearest element in a Matrix
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
- Find the distance of a point $P( x, y)$ from the origin.
- If the point $P (x, y)$ is equidistant from the points $A (5, 1)$ and $B (1,5)$, prove that $x = y$.
- Python program to convert complex number to polar coordinate values
- Find the value of $p$ such that the pair of equations ( 3 x-y-5=0 ) and ( 6 x-2 y-p=0 ), has no solution.
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Check whether the point (x, y) lies on a given line in Python
