Manhattan Distance: Walking the Street Grid

Linear AlgebraEasy+10 XP+10

Manhattan Distance: Walking the Street Grid

Manhattan distance (the L1 norm of the difference) adds up the gaps along each axis — the distance you'd walk on a street grid where you can't cut diagonally across a block.

1d = Σ |a[i] - b[i]|

Task: write manhattan_distance(point_a, point_b) returning the distance, rounded to 4 decimal places.

  • The points always have the same number of dimensions.
  • Two identical points are 0 apart.

L1 turns up wherever you want to discourage large individual coordinates without squaring them. Lasso regularisation is exactly this distance applied to a weight vector, and the reason it drives weights to exactly zero rather than merely small.

Bird's Flight vs. Taxi Route

Two ways to measure the distance between the same two points on a city grid.

AB
A bird flies straight from A to B: distance = 7.62 blocks. This is Euclidean distance.

Examples

point_a = [1.0, 2.0]
point_b = [4.0, 6.0]
returns 7.0
point_a = [0.0, 0.0, 0.0]
point_b = [1.0, -1.0, 2.0]
returns 4.0

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run