Intermediate · Lists · 130 of 264
Zeros to the end
Move every 0 to the end of the list, keeping the other numbers in their order.
Input: whole numbers on one line. Output: the reordered numbers.
Examples
Input
0 1 0 3 12
Output
1 3 12 0 0
Input
1 2 3
Output
1 2 3
Input
0 0 1
Output
1 0 0
One more test checks other inputs when you press Check.
Stuck? The idea behind lists
A list keeps several values in order: [3, 9, 1]. Its items are numbered from 0, like a string's characters.
append() adds to the end; sum(), min(), max(), len() and sorted() work on a whole list. Read numbers from one line with input().split() and int().
numbers = [int(x) for x in input().split()] print(sum(numbers), max(numbers)) numbers.append(0) print(numbers)
Watch for: split() gives text: "10" sorts before "9". Turn each item into a number before comparing or adding.