물음표 살인마의 개발블로그

알고리즘 문제/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

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

RGB to Hex Conversion  (0) 2022.10.06
Human Readable Time  (0) 2022.10.05
Printer Errors  (1) 2022.10.05
Does my number look big in this?  (0) 2022.10.05
Find the next perfect square!  (0) 2022.10.05