【每日一题20220711】移动所有0到末尾

:mage:‍编写一个算法,该算法采用一个数组并将所有零移动到末尾,同时保留其他元素的顺序。

move_zeros([1, 0, 1, 2, 0, 1, 3]) # returns [1, 1, 2, 1, 3, 0, 0]

题目难度:简单
题目来源:https://www.codewars.com/kata/52597aa56021e91c93000cb0

def move_zeros(lst: list) -> list:
    return lst

assert move_zeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]) == [1, 2, 1, 1, 3, 1, 0, 0, 0, 0]
assert move_zeros([9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]) == [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert move_zeros([0, 0]) == [0, 0]
assert move_zeros([0]) == [0]
assert move_zeros([]) == []
def move_zeros(lst: list) -> list:
    if len(lst)>0:
        for k in range(lst.count(0)):
            for i in range(len(lst)-1):
                if lst[i] == 0:
                    lst[i],lst[i+1] = lst[i+1],lst[i]
        return lst
    else:
        return []



if __name__ == '__main__':
    assert move_zeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]) == [1, 2, 1, 1, 3, 1, 0, 0, 0, 0]
    assert move_zeros([9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]) == [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    assert move_zeros([0, 0]) == [0, 0]
    assert move_zeros([0]) == [0]
    assert move_zeros([]) == []
1 个赞
def move_zeros(lst: list) -> list:
    res = []
    zero_count = 0
    for i in lst:
        if i != 0:
            res.append(i)
        else:
            zero_count += 1
    return res + [0, ] * zero_count
1 个赞
def move_zeros(lst: list) -> list:
    lst2=[]
    zero_list=[]
    if len(lst)==0:
        return lst
    else:
        for i in range(len(lst)):
            if lst[i]!=0:
                lst2.append(lst[i])
            elif lst[i]==0:
                zero_list.append(0)
        lst2.extend(zero_list)
        return lst2




assert move_zeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]) == [1, 2, 1, 1, 3, 1, 0, 0, 0, 0]
assert move_zeros([9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]) == [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert move_zeros([0, 0]) == [0, 0]
assert move_zeros([0]) == [0]
assert move_zeros([]) == []
def move_zeros(lst):
    l=r=0
    while r<len(lst):
        if lst[l]!=0:
            l+=1
        elif lst[r]!=0:
            lst[l],lst[r]=lst[r],lst[l]
            l+=1
        r+=1
    return lst

def move_zeros(lst: list) -> list:
    len_lst = len(lst)
    if len_lst > 0:
        zero_lst = [0] * lst.count(0)
        non_zero_list = list(filter(lambda x: x != 0, lst))
        return non_zero_list+zero_lst
    else: return []
def move_zeros(lst: list) -> list:
    for item in lst:
        if item == 0:
            lst.remove(item)
            lst.append(0)
    return lst

assert move_zeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]) == [1, 2, 1, 1, 3, 1, 0, 0, 0, 0]
assert move_zeros([9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]) == [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert move_zeros([0, 0]) == [0, 0]
assert move_zeros([0]) == [0]
assert move_zeros([]) == []
1 个赞
def move_zeros(lst: list) -> list:
    num = lst.count(0)
    for i in range(num):
        lst.append(lst.pop(lst.index(0)))
    return lst

assert move_zeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]) == [1, 2, 1, 1, 3, 1, 0, 0, 0, 0]
assert move_zeros([9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]) == [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert move_zeros([0, 0]) == [0, 0]
assert move_zeros([0]) == [0]
assert move_zeros([]) == []
def move_zeros(lst: list) -> list:
    num_list=[i for i in lst if i !=0]
    return num_list+[0]*lst.count(0)
def move_zeros(lst: list) -> list:
    zero_count = lst.count(0)
    res_str = ''.join([str(lst[i]) for i in range(len(lst))]).replace('0', '') + '0' * zero_count
    return [int(res_str[i]) for i in range(len(res_str))]

assert move_zeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]) == [1, 2, 1, 1, 3, 1, 0, 0, 0, 0]
assert move_zeros([9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]) == [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0,0, 0, 0, 0, 0, 0, 0, 0]
assert move_zeros([0, 0]) == [0, 0]
assert move_zeros([0]) == [0]
assert move_zeros([]) == []