- 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
3 Antworten
+ 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.
+ 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