Python | Farthest point on horizontal lines in 2D plane

Sometimes, while in competitive programming, we might be facing a problem which is of geometry domain and works with x-y coordinate system. The list of tuple can be used to store the same. And along with this, there might be a problem in which we need point with max value of x axis with similar y axis i.e farthest point on horizontal lines. Let’s discuss certain ways to discuss this problem.

Method : Using list comprehension + max()
This is a generic brute force method applied to get the max x axis point for common y axis, made as 1 liner using list comprehension. The max() is used to find the max of x axis element.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Farthest point on horizontal lines in 2D plane
# Using list comprehension + max()
from collections import defaultdict
  
# initializing list
test_list = [(1, 6), (4, 6), (2, 6), (6, 8), (1, 8), (2, 9)]
  
# printing original list
print("The original list is : " +  str(test_list))
  
# Using list comprehension + max()
# Farthest point on horizontal lines in 2D plane
temp = defaultdict(list)
for key, val in test_list:
   temp[val].append(key)
res = [(key, val) for key, val in test_list if max(temp[val]) == key]
  
# Printing result
print("The list after filtering just maximum points on lines : " +  str(res))

chevron_right


Output :

The original list is : [(1, 6), (4, 6), (2, 6), (6, 8), (1, 8), (2, 9)]
The list after filtering just maximum points on lines : [(4, 6), (6, 8), (2, 9)]


My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.