Zeros to the back
Inputs
values is a list of 0 to 14 whole numbers, each from 0 to 6.
Your tasks
- Create a list
movedand a countzerosof 0. - Use a loop over
values. Add each non-zero number tomoved, and add 1 tozerosfor each 0. - After the loop, add
zeroszeros to the end ofmoved. - Complete
zerosToBackso it returnsmoved. Return it, do not print it.
Example
With values = [0, 3, 0, 5, 2], zerosToBack returns [3, 5, 2, 0, 0].
Rules
- Non-zero numbers keep their order.
Test data
TEST DATA 1: values is [0, 3, 0, 5, 2]
TEST DATA 2: values is []
Practice focus
Two pointers and sliding windows: loops that move two positions instead of one. Two pointers and sliding windows turn many quadratic answers into linear ones.