【每日一题1105】楼层距离

:woman_mage: 有一天,哈利波特来到一座城堡出席演讲活动,但是会场的指示牌被捣蛋鬼施了魔法。结果根据路线指示,他从5楼乘电梯下到2楼,然后又上到8楼,这才抵达了正确的会场。他所乘坐电梯经历的楼层距离一共是3+6=9层。活动的嘉宾非常多,请编写一个函数,给定嘉宾乘坐楼层的数字列表nums,计算出他所经历的楼层距离。

示例:
输入:[5, 2, 8],输出:9。
输入:[7, 1, 7, 1],输出:18。

题目难度:简单
题目来源:codewars: Elevator Distance

def floors(nums: list) -> int:
    pass

assert floors([5, 2, 8]) == 9
assert floors([1, 2, 3]) == 2
assert floors([7, 1, 7, 1]) == 18
assert floors([3, 3]) == 0
def floors(nums:list):
    res=0
    if len(nums)==1:
        return "嘉宾未移动"
    else:
        for i in range(len(nums)-1):
            res=res+abs(nums[i]-nums[i+1])
    return res
def floors(nums: list) -> int:
    return sum([abs(nums[i] - nums[i-1]) for i in range(1, len(nums))])


assert floors([5, 2, 8]) == 9
assert floors([1, 2, 3]) == 2
assert floors([7, 1, 7, 1]) == 18
assert floors([3, 3]) == 0
def floors(self, nums: list) -> int:
    '''
    有一天,哈利波特来到一座城堡出席演讲活动,但是会场的指示牌被捣蛋鬼施了魔法。结果根据路线指示,他从5楼乘电梯下到2楼,然后又上到8楼,这才抵达了正确的会场。他所乘坐电梯经历的楼层距离一共是3+6=9层。活动的嘉宾非常多,请编写一个函数,给定嘉宾乘坐楼层的数字列表nums,计算出他所经历的楼层距离。

    示例:
    输入:[5, 2, 8],输出:9。
    输入:[7, 1, 7, 1],输出:18。
    :return:
    '''
    '''
    1. 楼层的高度没有给出, 比如楼层一共20层,隐含条件是数字只能取1~20
    2. 给定的list也没有约束最大值,比如在1-20之间来回取值,约定最大值是10
    
    '''
    result = 0
    nums_len = len(nums)
    if nums_len == 0 or nums_len > 10 or type(nums[0]) != int or nums[0] > 20:
        return 0
    for i in range(0, nums_len - 1):
        if type(nums[i+1]) == int and nums[i+1] <= 20:
            result += abs(nums[i + 1] - nums[i])
        else:
            return 0
    return result
    pass

def test_floors(self):
    '''
    用例设计按照上面
    :return:
    '''
    assert self.floors([5, 2, 8]) == 9
    assert self.floors([7, 1, 7, 1]) == 18
    # 长度为0
    assert self.floors([]) == 0
    # 长度为1,且楼层数是1
    assert self.floors([1]) == 0
   # 长度为10, 且楼层数包含19 ,20,数列中包含相同数的有效边界值
    assert self.floors([1,2,3,4,19,6,7,7,5,20]) == 49
    # 长度为11, 且楼层数包含19 ,20的无效值,
    assert self.floors([1,2,3,4,19,6,7,8,5,20, 20]) == 0
    # 楼层数包含21的无效值,
    assert self.floors([5, 7, 9, 21, 7]) == 0
    # 楼层数包含非数字的无效值,
    assert self.floors([5, 7, 9.0, 21, 7]) == 0
    # 楼层数包含非数字的无效值,
    assert self.floors([5, 7, "9", 21, 7]) == 0
1 个赞
    public Integer floors(ArrayList<Integer> arrayList){
        Integer res = 0;
        for (int i = 0; i < arrayList.size()-1; i++) {
            res += Math.abs(arrayList.get(i+1)-arrayList.get(i));
        }
        return res;
def floors(nums):
    length = len(nums)
    result = 0
    for i in range(length-1):
        result += abs(nums[i]-nums[i+1])
    #     floor = nums[i]-nums[i+1]
    #     if floor>=0:
    #         result += floor
    #     else:
    #         result -= floor
    return result

列表中没有数据或者只有一个数据时,代表哈利未移动
当列表中的数据大于一个时,abs(nums[i]-nums[i+1])就代表哈利一次移动的层数,将每次移动的层数相加即可得到哈利经历的楼层距离
注:注释的代码是不使用abs函数的情况

1 个赞
def floors(nums: list) -> int:
    floors_nums = 0
    for i in range(len(nums)-1):
        floors_nums += abs(nums[i] - nums[i + 1])
    return floors_nums
1 个赞
def get_floors(nums:list)->int:
    floors = 0
    for i in range(len(nums)-1):
        num = abs(nums[i+1] - nums[i])
        floors = floors + num
    return floors
1 个赞
def floors(nums: list) -> int:
    list1=[]
    for i in range(1,len(nums)):
        x=nums[i]-nums[i-1]
        if x<0:
            x=x*(-1)
        list1.append(x)
    return sum(list1)

assert floors([5, 2, 8]) == 9
assert floors([1, 2, 3]) == 2
assert floors([7, 1, 7, 1]) == 18
assert floors([3, 3]) == 0
def floors(nums: list) -> int:
    return sum(abs(nums[i+1] -nums[i]) for i in range(len(nums)-1))

assert floors([5, 2, 8]) == 9
assert floors([1, 2, 3]) == 2
assert floors([7, 1, 7, 1]) == 18
assert floors([3, 3]) == 0
1 个赞
def floors(nums: list) -> int:
    sum = 0
    for i in range(0, len(nums) - 1):
        sum += abs(nums[i] - nums[i + 1])
    return sum


assert floors([5, 2, 8]) == 9
assert floors([1, 2, 3]) == 2
assert floors([7, 1, 7, 1]) == 18
assert floors([3, 3]) == 0
def floors(nums: list) -> int:
    return sum(abs(nums[i]-nums[i+1]) for i in range(len(nums)-1))
def solution(nums: list):
    res = []
    for i in range(0, len(nums)):
        if i == len(nums) -1:
            break
        if nums[i] < nums[i+1]:
            res.append(nums[i+1] - nums[i])
            continue
        res.append(nums[i] - nums[i+1])
    return sum(res)


assert solution([5, 2, 8]) == 9
assert solution([1, 2, 3]) == 2
assert solution([7, 1, 7, 1]) == 18
assert solution([3, 3]) == 0