【每日一题20220222】曼哈顿距离

:woman_mage: 我们都知道,通过距离公式可以计算出地图上两点的直接距离。但是,如果说在连线上有其他建筑物呢?那么我们要换个公式:曼哈顿距离公式。

曼哈顿距离是将两点看作是分布在地图中的网格上(有点像棋盘),所以它们之间的距离是横向和纵向路径(不能是斜线)的长度总和。

请编写一个函数,接收2个点的坐标,请计算并返回它们之间的曼哈顿距离。

示例:
输入:[1, 1], [0, 3],输出:3
输入:[5, 4], [3, 2],输出:4

题目难度:中等
题目来源:CodeWars: Manhattan Distance

def solution(point_a: list, point_b: list) -> int:
    # your code here

assert solution([1,1],[0,3]) == 3
assert solution([5,4],[3,2]) == 4
assert solution([1,1],[1,1]) == 0
def solution(point_a: list, point_b: list) -> int:
    return abs(point_a[0]-point_b[0])+abs(point_a[1]-point_b[1])

assert solution([1, 1], [0, 3]) == 3
assert solution([5, 4], [3, 2]) == 4
assert solution([1, 1], [1, 1]) == 0
def solution(point_a: list, point_b: list) -> int:
    # your code here
    return abs(point_a[0] - point_b[0]) + abs(point_a[1] - point_b[1])


assert solution([1, 1], [0, 3]) == 3
assert solution([5, 4], [3, 2]) == 4
assert solution([1, 1], [1, 1]) == 0
def solution(point_a: list, point_b: list) -> int:
    return abs(point_a[0]-point_b[0])+abs(point_a[1]-point_b[1])

assert solution([1,1],[0,3]) == 3
assert solution([5,4],[3,2]) == 4
assert solution([1,1],[1,1]) == 0
from math import fabs


def solution(point_a: list, point_b: list) -> int:
    return int(fabs(point_a[0] - point_b[0])) + int(fabs(point_a[1] - point_b[1]))


assert solution([1, 1], [0, 3]) == 3
assert solution([5, 4], [3, 2]) == 4
assert solution([1, 1], [1, 1]) == 0

c = abs(int(point_a[0])-int(point_b[0])) + abs(int(point_a[1])-int(point_b[1]))
return c

def solution(point_a: list, point_b: list) -> int:
    # your code here
    return abs(point_a[0]-point_b[0])+abs(point_a[1]-point_b[1])
def solution(point_a: list, point_b: list) -> int:
    return abs(point_a[0]-point_b[0])+abs(point_a[1]-point_b[1])

assert solution([1,1],[0,3]) == 3
assert solution([5,4],[3,2]) == 4
assert solution([1,1],[1,1]) == 0
def solution(point_a: list, point_b: list) -> int:
    return abs(point_a[0] - point_b[0]) + abs(point_a[1] - point_b[1])


assert solution([1, 1], [0, 3]) == 3
assert solution([5, 4], [3, 2]) == 4
assert solution([1, 1], [1, 1]) == 0

练习

def solution(point_a: list, point_b: list) -> int:
     w, h = abs(point_a[0] - point_b[0]), abs(point_a[1] - point_b[1])
     return w + h

assert solution([1,1],[0,3]) == 3
assert solution([5,4],[3,2]) == 4
assert solution([1,1],[1,1]) == 0
def solution(point_a: list, point_b: list) -> int:
    result = abs(point_a[0] - point_b[0]) + abs(point_a[1] - point_b[1])
    return result


assert solution([1, 1], [0, 3]) == 3
assert solution([5, 4], [3, 2]) == 4
assert solution([1, 1], [1, 1]) == 0
public static int solution(int[] a,int[] b){
      return Math.abs(a[0]-b[0])+Math.abs(a[1]-b[1]);
}
    def solution(point_a: list, point_b: list) -> int:
        a = point_a[0] - point_b[0]
        b = point_a[1] - point_b[1]
        return (-a if a < 0 else a) + (-b if b < 0 else b)
def solution(point_a: list, point_b: list) -> int:
    obj = list(zip(point_a, point_b))
    return sum(
        [obj[i][0] - obj[i][1] if obj[i][0] - obj[i][1] >= 0 else obj[i][1] - obj[i][0] for i in range(len(obj))])


assert solution([1, 1], [0, 3]) == 3
assert solution([5, 4], [3, 2]) == 4
assert solution([1, 1], [1, 1]) == 0