CodeWars:Moving Zeros To The End
Challenge Name
Moving Zeros to The End
Challenge Description:
Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
Challenge link
https://www.codewars.com/kata/52597aa56021e91c93000cb0/train/python
Example:
move_zeros([1, 0, 1, 2, 0, 1, 3]) # returns [1, 1, 2, 1, 3, 0, 0]
Code:
def move_zeros(array):
c=0
op=[]
for i in array:
if i==0:
c+=1
pass
else:
op.append(i)
for i in range(c):
op.append(0)
return op
Explanation:1.Intilaize variable c=0 and empty list op2.Iterate every element in array3.If any element is 0 increment c by 14.else append element to list op5.using for loop add 0 to list op 'c' times6.return opSolution not working !! or any other suggestions please comment !
0 Comments
Post a Comment