Editor

Closest to the Lighthouse

#213 · Geometry · Beginner · 30 XP · about 6 min

A lighthouse stands at the point (0, 0), and boats report their positions as (x, y) tuples of integers. Write closest_point(points) that returns the position closest to the lighthouse, measured in a straight line.

If several positions are equally close, return the one that comes first in the list. Return the tuple exactly as it appears in the list, and return None for an empty list.

Examples

closest_point([(3, 4), (-2, 2), (5, 0)])   # returns (-2, 2)
closest_point([(1, -2), (2, 1), (0, 3)])   # returns (1, -2)
closest_point([])                          # returns None

In the second example, (1, -2) and (2, 1) are both the square root of 5 away from the lighthouse. (1, -2) comes first, so it wins.

Constraints

  • 0 to 10,000 positions.
  • Coordinates are between -10,000 and 10,000.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.