""" Moving Zeros To The End
алгоритм, который берет массив и перемещает все нули в конец, сохраняя порядок остальных элементов """



import time



def move_zeros(s):
    list_new = []
    list_0 = []
    for s1 in s:
        if s1 == 0:
            list_0.append(0)
        else:
            list_new.append(s1)
    
    for s0 in list_0:
        list_new.append(s0)
    
    return list_new



if __name__ == '__main__':
    #move_zeros([1, 0, 1, 2, 0, 1, 3]) # returns [1, 1, 2, 1, 3, 0, 0]
    start_time = time.time()
    print(move_zeros([1, 0, 1, 2, 0, 1, 3]), "Время выполнения: ", time.time() - start_time)

 Public
Share a link to this review

13.79% issue ratio

R1 Missing type hints

Type hints help humans and linters (like mypy) to understand what to expect "in" and "out" for a function. Not only it serves as a documentation for others (and you after some time, when the code is wiped from your "brain cache"), but also allows using automated tools to find type errors.

R19 Bad variable name

Simple rule: read a variable name as if you were English-speaking person without a context and guess what's inside - and if you have no clue, then you should give it a better name. Exceptions: i, j, x, y etc.

s and s1 are not good variable names. Should be list_mixed and element, for example.

L9 Bad design

Seems like things could be organized in a better way.

list_0 makes no sense because it always contains zeros. So instead of storing [0, 0, 0, 0, ...] you could just store number of zeros you found. With this knowledge, you could write the whole thing without actually using list_0 and list_new: count number of zeros in s, remove them from s, add required number of zeros to what's left from s. See code below.

Suggested change:
def move_zeros(s):
    num_items = len(s)
    s_without_zeros = [item for item in s if item != 0]
    num_zeros = num_items - len(s_without_zeros)
    s_with_zeros_in_the_end = s_without_zeros + [0] * num_zeros
    return s_with_zeros_in_the_end
L12 Redundant code / overengineering

This code is not really needed or may be simplified

Suggested change:
list_new += list_0

Create new review request