livepython.net

217. Python Program to Move All Zeros to the End of a List

MediumPhase 2 · Practice

This takes a few steps. A for loop, append() and a counter do it, and you know all three.

Overview

Moving the zeros to the end is a common interview question about lists. The tricky part is to keep the other numbers in their order.

Problem Statement

Ivan keeps the points of each player in a game, in the order they played. Ivan wants the players with 0 points moved to the end of the list.

  • Move every 0 to the end of the list.
  • The other numbers keep their order. Do not sort them.
  • Print Result: <numbers>, with one space between the numbers.

Examples

Example 1. Input 0 1 0 3 12 gives:

Result: 1 3 12 0 0

Why: the two zeros go to the end. 1, 3 and 12 keep their order.

Example 2. Input 5 4 3 gives:

Result: 5 4 3

Why: there is no 0, so nothing moves. The numbers are not sorted.

Input

One line of whole numbers, each 0 or more, with one space between them. The line has 1 number or more. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line: Result:, a space, and the numbers with one space between them. The tests try 7 different inputs, also only zeros and a single number.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 12 · Working With Strings

This is the last question of Phase 2 · Practice. The next question starts Phase 3.

solution.py
# write your code here

EXPECTED OUTPUT

1Result: 1 3 12 0 0

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.