← Back to challenges

Mutations Only: Zeroes to the End

PythonHardarrayslanguage_fundamentals

Instructions

Write a function that moves all the zeroes to the end of a list. Do this without returning a copy of the input list.

Examples

zeroes_to_end([1, 2, 0, 0, 4, 0, 5]) ➞ [1, 2, 4, 5, 0, 0, 0]

zeroes_to_end([0, 0, 2, 0, 5]) ➞ [2, 5, 0, 0, 0]

zeroes_to_end([4, 4, 5]) ➞ [4, 4, 5]

zeroes_to_end([0, 0]) ➞ [0, 0]

Notes

  • You must mutate the original list.
  • Keep the relative order of the non-zero elements the same.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.