Sum Distances in a Nested Tuple of Points

Medium ⏱ 12 min 50% acceptance ★★★★☆ 4.3
Given path = ((x1, y1), (x2, y2), ...), a tuple of coordinate tuples describing a polyline, implement total_manhattan_distance(path) returning the sum of Manhattan distances between each consecutive pair of points, using nested tuple unpacking in a loop (for (x1, y1), (x2, y2) in zip(path, path[1:])).

Examples

Example 1
Input
total_manhattan_distance(((0, 0), (3, 0), (3, 4)))
Output
7
Explanation

Distance (0,0)->(3,0) is 3, plus (3,0)->(3,4) is 4, total 7.

Constraints

  • path has at least 2 points.

Topics

TuplesNested Data

Companies

OlaUberSwiggy

Hints

Hint 1

Manhattan distance between two points is abs(x2-x1) + abs(y2-y1).

Hint 2

zip(path, path[1:]) pairs each point with the next one.

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