알고리즘 문제/CodeWar

Moving Zeros To The End

BEstyle 2022. 10. 5. 13:06

DESCRIPTION:

Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.

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

def move_zeros(lst):
    alist=[]
    count=0
    for i in lst:
        if i!=0:
            alist.append(i)
        else:
            count+=1
    for i in range(count):
        alist.append(0)
    return alist