""" 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)
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.
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:
s1 are not good variable names. Should be
element, for example.
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_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.
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 +  * num_zeros return s_with_zeros_in_the_end
This code is not really needed or may be simplified
list_new += list_0
Create new review request