Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-031

Minkowski Distance

Problem

Compute the Minkowski distance of order p >= 1: (sum(|a_i - b_i|^p))^(1/p).

p = 1 gives Manhattan distance and p = 2 gives Euclidean distance. Empty vectors return 0.0.

Hint

Raise coordinate differences to p, sum, then take the p-th root.

Test cases

case 1
in: [[0,0],[3,4],2]
expected: 5
case 2
in: [[2,2],[5,6],1]
expected: 7
case 3
in: [[1,2],[4,6],3]
expected: 4.497941445275415
case 4
in: [[1,2,3],[1,2,3],5]
expected: 0
Starter codePython
def minkowski_distance(a, b, p):
    # Your code here
    pass

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