MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / manhattan_distance_one_liner

Function manhattan_distance_one_liner

maths/manhattan_distance.py:81–120  ·  view source on GitHub ↗

Version with one liner >>> manhattan_distance_one_liner([1,1], [2,2]) 2.0 >>> manhattan_distance_one_liner([1.5,1.5], [2,2]) 1.0 >>> manhattan_distance_one_liner([1.5,1.5], [2.5,2]) 1.5 >>> manhattan_distance_one_liner([-3, -3, -3], [0, 0, 0]) 9.0 >>> manhat

(point_a: list, point_b: list)

Source from the content-addressed store, hash-verified

79
80
81def manhattan_distance_one_liner(point_a: list, point_b: list) -> float:
82 """
83 Version with one liner
84
85 >>> manhattan_distance_one_liner([1,1], [2,2])
86 2.0
87 >>> manhattan_distance_one_liner([1.5,1.5], [2,2])
88 1.0
89 >>> manhattan_distance_one_liner([1.5,1.5], [2.5,2])
90 1.5
91 >>> manhattan_distance_one_liner([-3, -3, -3], [0, 0, 0])
92 9.0
93 >>> manhattan_distance_one_liner([1,1], None)
94 Traceback (most recent call last):
95 ...
96 ValueError: Missing an input
97 >>> manhattan_distance_one_liner([1,1], [2, 2, 2])
98 Traceback (most recent call last):
99 ...
100 ValueError: Both points must be in the same n-dimensional space
101 >>> manhattan_distance_one_liner([1,"one"], [2, 2, 2])
102 Traceback (most recent call last):
103 ...
104 TypeError: Expected a list of numbers as input, found str
105 >>> manhattan_distance_one_liner(1, [2, 2, 2])
106 Traceback (most recent call last):
107 ...
108 TypeError: Expected a list of numbers as input, found int
109 >>> manhattan_distance_one_liner([1,1], "not_a_list")
110 Traceback (most recent call last):
111 ...
112 TypeError: Expected a list of numbers as input, found str
113 """
114
115 _validate_point(point_a)
116 _validate_point(point_b)
117 if len(point_a) != len(point_b):
118 raise ValueError("Both points must be in the same n-dimensional space")
119
120 return float(sum(abs(x - y) for x, y in zip(point_a, point_b)))
121
122
123if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

_validate_pointFunction · 0.85

Tested by

no test coverage detected