Nearest Delivery Hub

Medium ⏱ 12 min 49% acceptance ★★★★★ 4.6
Hubs are (name, x, y) tuples. Write nearest_hub(hubs, px, py) returning the name of the hub closest to the point (px, py) by straight-line distance, using min with a lambda key. No need to take a square root — comparing squared distances gives the same winner.

Examples

Example 1
Input
nearest_hub([('North', 0, 5), ('East', 2, 1)], 0, 0)
Output
'East'
Explanation

East is √5 away; North is 5 away.

Constraints

  • Use min with key=.
  • Assume at least one hub.

Topics

Lambda & Functionalmin with key

Companies

SwiggyZomatoAmazon

Hints

Hint 1

Squared distance: (x-px)**2 + (y-py)**2.

Hint 2

Read [0] off the winning tuple.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.