Write an algorithm that takes an list and moves all of the zeros to the end, preserving the order of the other elements.
move_zeros([False,1,0,1,2,0,1,3,"a"]) # returns[False,1,1,2,1,3,"a",0,0]
For test exemples:
[1,2,0,1,0,1,0,3,0,1]
[9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9]
["a",0,0,"b","c","d",0,1,0,1,0,3,0,1,9,0,0,0,0,9]
["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9]
[0,1,None,2,False,1,0]
["a","b"]
["a"]
[0,0]
[0]
[False]
[]
On Python
Answers & Comments
arr = input("Enter the array separated by a space: ").split()
count = 0
print(arr)
def sort(arr, count = 0):
"""The function sorts the array using the bubble algorithm"""
point = len(arr)
while True:
if count == point - 1:
count = 0
point -= 1
elif point == 1:
break
if arr[count] == "0":
arr[count + 1],arr[count] = arr[count], arr[count + 1]
count += 1
return arr
sort(arr)
print(arr)