please tell me its python code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

please tell me its python code

A list of integers is given. It is required to "shrink" it by moving all non-zero elements to the left side of the list without changing their order, and all zeros to the right side. The order of the non-zero elements cannot be changed, an additional list cannot be used, and the task must be completed in one pass through the list. Print the resulting list. Example input data: 4 0 5 0 3 0 0 5 Example result: 4 5 3 5 0 0 0 0

15th Mar 2022, 6:38 PM
saad
3 Answers
+ 6
saad , please show your code attempt. you have joined sololearn about 7 minutes ago, and you have not done any tutorial here in sololearn. so this exercise is a good starting point for stepping in python. thanks!
15th Mar 2022, 6:41 PM
Lothar
Lothar - avatar
+ 3
Hi! Maybe it easiest to first make a list of ints, count the zeros, and then use filter to get them away. In the end you add your zeros again at the end of the list and use join to make them to a string again. An even easier way: Even r: str -> list Count ‘0’s Use a list comprehension with if statment to get rid if the ‘0’s and then, add the ‘0’s to the end of the list. Use joint to make a string again.
15th Mar 2022, 6:56 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
You might find inspiration from solutions posted in this challenge that is very similar to your problem: https://www.sololearn.com/post/1269153/?ref=app
15th Mar 2022, 6:55 PM
Brian
Brian - avatar