All problems
General p-Norm of a Vector
Problem
Compute the p-norm of a vector:
||v||_p = (sum_i |v[i]|^p)^(1/p)
Return None if p <= 0. For p = 1 this is the taxicab norm and for p = 2 the Euclidean norm.
Hint
Raise absolute values to p, sum, then take the p-th root.
Test cases
case 1
in: [[3,4],2]
expected: 5
case 2
in: [[1,-2,2],1]
expected: 5
case 3
in: [[1],5]
expected: 1
case 4
in: [[0,0],3]
expected: 0
case 5
in: [[1,2],0]
expected: null
Starter codePython
def general_p_norm(v, p):
# Your code here
pass5 test cases · run your code against them in the editor