How to write a function to the task: "Write a function that returns the distance (Minkowski distance) of order p between two points whose coordinates are input arguments. The value of order p is default 2."?

def nth_root(value, n_root): root_value = 1/float(n_root) return round ((value) ** (root_value),3) def minkowski_distance(x,y,p=2): return nth_root(sum(pow(abs(ab),p) for a,b in zip(x, y)),p) print minkowski_distance([1, 1, 1], [3, 3, 0]) print minkowski_distanceprint minkowski_distance([2, 2, 3, 5], [6, 1, 8, 2], 4) 
  • one
    and what exactly you can not? where are the difficulties? - MaxU
  • SO is not a site where they write the code for your order. Show what you have done and what you can’t do. - Petr Abdulin Sep.
  • You did not even try to do something yourself. You are asking to complete the task for you. With such a formulation of the question, someone else will also receive an assessment for the assignment - virvaldium September
  • one
  • one
    @PetrAbdulin I understood, I'm sorry, I just didn’t want to lay out the code initially, now I’ll always do this no matter what it is. - Andrey Sindeev

2 answers 2

 def minkowski(a, b, p=2): assert len(a) == len(b), "'a' and 'b' must be of the same length" return pow(sum((abs(xy)**p) for x,y in zip(a,b)), 1/p) 

Test:

 In [102]: print(minkowski([1, 1, 1], [3, 3, 0])) 3.0 In [105]: print(minkowski([1, 1, 1], [3, 3, 0], 4)) 2.39678172692843 
  • Thank you very much, very interesting! - Andrey Sindeev
  • @AndreySindeev, always happy to help :) - MaxU
 def nth_root(value, n_root): root_value = 1/float(n_root) return round ((value) ** (root_value),3) def minkowski_distance(x,y,p=2): return nth_root(sum(pow(abs(ab),p) for a,b in zip(x, y)),p) print minkowski_distance([1, 1, 1], [3, 3, 0]) print minkowski_distanceprint minkowski_distance([2, 2, 3, 5], [6, 1, 8, 2], 4)