Move Zeros In Place
Write move_zeros(items) that moves every item equal to 0 to the end of the list, keeping the order of the other items. It must change the list in place and return None: the caller looks at the list it passed in.
After data = [0, 1, 0, 3, 12] and move_zeros(data), data is [1, 3, 12, 0, 0].
Understand the problem
Learn the difference between rebinding and changing.
What does the caller see?
def f(a):
a = [1, 2, 3]
data = [9]
f(data)
print(data)
[1, 2, 3][9]
a = [1, 2, 3] makes the local name a point at a new list. The caller's data still points at the old list, so it prints [9]. Assignment to a name never changes the caller's object.
Which statement changes the list that a refers to, so that the caller sees the change?
a = [1, 2, 3]a[:] = [1, 2, 3]b = a + [4]
a[:] = ... is slice assignment: it replaces the contents of the existing list object. a.append(x), a.sort() and a[0] = x also change the object in place.
Write the solution
Change the list in place; do not return anything.
def move_zeros(items):
# Write your code here.
pass
Hint 1: a question to ask yourself
If you build a new list with the non-zeros first and the zeros after, how do you put it back into the same list object?
Hint 2: the shape of the answer
Collect the non-zero items in a list, count the zeros, then items[:] = nonzeros + [0] * zeros. Or walk along with a write position and fill the end with zeros.
Reflect
Solution: slice assignment
def move_zeros(items):
nonzeros = [x for x in items if x != 0]
items[:] = nonzeros + [0] * (len(items) - len(nonzeros))
Another way: two positions, no second list
def move_zeros(items):
write = 0
for x in items:
if x != 0:
items[write] = x
write += 1
for i in range(write, len(items)):
items[i] = 0
What this tests: in-place modification, slice assignment (items[:] = ...), the difference between rebinding a name and changing an object, and the two-pointer technique.
Why items = [...] would be a bug: it only rebinds the local name. The caller's list is untouched. This is one of the most common real-world Python mistakes, and a favourite question.
Return None: Python's own in-place methods (list.sort, list.reverse, list.append) return None to tell the reader that the object was changed. Follow that convention.
Cost: O(n) time. The first version uses O(n) extra space; the second uses O(1), which is what "in place" usually means in an interview.
What an interviewer might ask next: Do it with only one pass and no second list. (Swap each non-zero with the item at the write position.)
VISIBLE = 3
CASES = [
('data = [0, 1, 0, 3, 12]\nmove_zeros(data)\nresult = data', [1, 3, 12, 0, 0], 'the list changes in place'),
('result = move_zeros([1, 0])', None, 'nothing is returned'),
('data = [0, 1]\nsame = data\nmove_zeros(data)\nresult = (same is data, same)', (True, [1, 0]), 'the same list object is changed'),
('data = []\nmove_zeros(data)\nresult = data', [], 'an empty list'),
('data = [0, 0, 0]\nmove_zeros(data)\nresult = data', [0, 0, 0], 'only zeros'),
('data = [5, 4, 3]\nmove_zeros(data)\nresult = data', [5, 4, 3], 'no zeros'),
('data = [4, 0, 2, 0, 1]\nmove_zeros(data)\nresult = data', [4, 2, 1, 0, 0], 'the order of the others is kept'),
('data = [0.0, 5, -1]\nmove_zeros(data)\nresult = data', [5, -1, 0.0], 'a float zero counts as zero'),
]