|
| 1 | +def manhattan_distance(point_a: list, point_b: list) -> float: |
| 2 | + """ |
| 3 | + Expectts two list of numbers representing two points in the same |
| 4 | + n-dimensional space |
| 5 | +
|
| 6 | + https://en.wikipedia.org/wiki/Taxicab_geometry |
| 7 | +
|
| 8 | + >>> manhattan_distance([1,1], [2,2]) |
| 9 | + 2.0 |
| 10 | + >>> manhattan_distance([1.5,1.5], [2,2]) |
| 11 | + 1.0 |
| 12 | + >>> manhattan_distance([1.5,1.5], [2.5,2]) |
| 13 | + 1.5 |
| 14 | + >>> manhattan_distance([-3, -3, -3], [0, 0, 0]) |
| 15 | + 9.0 |
| 16 | + >>> manhattan_distance([1,1], None) |
| 17 | + Traceback (most recent call last): |
| 18 | + ... |
| 19 | + ValueError: Missing an input |
| 20 | + >>> manhattan_distance([1,1], [2, 2, 2]) |
| 21 | + Traceback (most recent call last): |
| 22 | + ... |
| 23 | + ValueError: Both points must be in the same n-dimensional space |
| 24 | + >>> manhattan_distance([1,"one"], [2, 2, 2]) |
| 25 | + Traceback (most recent call last): |
| 26 | + ... |
| 27 | + TypeError: Expected a list of numbers as input, found str |
| 28 | + >>> manhattan_distance(1, [2, 2, 2]) |
| 29 | + Traceback (most recent call last): |
| 30 | + ... |
| 31 | + TypeError: Expected a list of numbers as input, found int |
| 32 | + >>> manhattan_distance([1,1], "not_a_list") |
| 33 | + Traceback (most recent call last): |
| 34 | + ... |
| 35 | + TypeError: Expected a list of numbers as input, found str |
| 36 | + """ |
| 37 | + |
| 38 | + _validate_point(point_a) |
| 39 | + _validate_point(point_b) |
| 40 | + if len(point_a) != len(point_b): |
| 41 | + raise ValueError("Both points must be in the same n-dimensional space") |
| 42 | + |
| 43 | + return float(sum(abs(a - b) for a, b in zip(point_a, point_b))) |
| 44 | + |
| 45 | + |
| 46 | +def _validate_point(point: list[float]) -> None: |
| 47 | + """ |
| 48 | + >>> _validate_point(None) |
| 49 | + Traceback (most recent call last): |
| 50 | + ... |
| 51 | + ValueError: Missing an input |
| 52 | + >>> _validate_point([1,"one"]) |
| 53 | + Traceback (most recent call last): |
| 54 | + ... |
| 55 | + TypeError: Expected a list of numbers as input, found str |
| 56 | + >>> _validate_point(1) |
| 57 | + Traceback (most recent call last): |
| 58 | + ... |
| 59 | + TypeError: Expected a list of numbers as input, found int |
| 60 | + >>> _validate_point("not_a_list") |
| 61 | + Traceback (most recent call last): |
| 62 | + ... |
| 63 | + TypeError: Expected a list of numbers as input, found str |
| 64 | + """ |
| 65 | + if point: |
| 66 | + if isinstance(point, list): |
| 67 | + for item in point: |
| 68 | + if not isinstance(item, (int, float)): |
| 69 | + raise TypeError( |
| 70 | + f"Expected a list of numbers as input, " |
| 71 | + f"found {type(item).__name__}" |
| 72 | + ) |
| 73 | + else: |
| 74 | + raise TypeError( |
| 75 | + f"Expected a list of numbers as input, found {type(point).__name__}" |
| 76 | + ) |
| 77 | + else: |
| 78 | + raise ValueError("Missing an input") |
| 79 | + |
| 80 | + |
| 81 | +def 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 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + import doctest |
| 125 | + |
| 126 | + doctest.testmod() |
0 commit comments