Unpack Coordinates Into a Distance Function

Medium ⏱ 12 min 61% acceptance ★★★★☆ 4.2
Write a function distance(x1, y1, x2, y2) that returns the Euclidean distance between points (x1, y1) and (x2, y2), rounded to 2 decimal places. Then, given two point tuples p1 and p2, call distance by unpacking both tuples with the * operator in a single call such as distance(*p1, *p2).

Examples

Example 1
Input
p1 = (0, 0)
p2 = (3, 4)
distance(*p1, *p2)
Output
5.0
Explanation

The classic 3-4-5 right triangle distance.

Constraints

  • distance must accept four separate positional numeric arguments.
  • Coordinates arrive packed in tuples and must be unpacked with * at the call site.

Topics

Functionsargument unpacking

Companies

UberOlaSwiggy

Hints

Hint 1

distance((x2-x1)**2 + (y2-y1)**2) ** 0.5 is the formula, before rounding.

Hint 2

Unpacking a tuple with *p1 inside a call spreads its elements as separate positional arguments.

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