I’ve the following array:
[[499, 3], [502, 3], [502, 353], [499, 353]]
They are the verteces of a rectangle.
I need to find the top-left, top-right, bottom-left and bottom-right vertex.
What’s the best python code to do it ?
thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
edit: thanks to tokand for pointing out that this can be done with tuple unpacking.
you could sort it.
or you could do it in place with
For reference, the output of sorted is:
This will be O(nlogn) whereas there are surely O(n) solutions available. But for a list of this size, I don’t think it’s a biggy unless you have a ton of them, (in which case, the speed of the native C implementation will outperform a custom python function anyways so it’s still optimal from a practical perspective.)