Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-291

L1 Distance Between Vectors

Problem

Sum the absolute differences of two equal-length vectors:

sum_i |a[i] - b[i]|

This is the L1 distance between the points. Empty inputs return 0.

Hint

Zip the vectors and accumulate absolute differences.

Test cases

case 1
in: [[1,2,3],[4,6,8]]
expected: 12
case 2
in: [[0,0],[0,0]]
expected: 0
case 3
in: [[],[]]
expected: 0
case 4
in: [[-1,-2],[1,2]]
expected: 6
case 5
in: [[2.5,0],[0,2.5]]
expected: 5
Starter codePython
def manhattan_distance(a, b):
    # Your code here
    pass

5 test cases · run your code against them in the editor