Skip to the content

The burnt cookies

Problem

The tray has just come out of the oven, and a few cookies got burnt. The baker writes down the weight of each cookie in grams, along the row, and marks the burnt ones with a 0. She wants the good ones at the front, in the order they were in, and the burnt ones at the back.

Write a function that takes the tray list, made of whole numbers from 0 up, and returns a new list: first the numbers that are not 0, in the same order they appear, and then as many 0s as there were.

With 12, 0, 9, 0 and 15 you return 12, 9, 15, 0, 0. You do not sort by weight: the 12 goes before the 9 because that is how it sat on the tray. If nothing is burnt, the list comes out the same. If everything is burnt, it comes out full of zeros. If it is empty, you return an empty list.

Examples

  • The example

    [12, 0, 9, 0, 15] → [12, 9, 15, 0, 0]

  • Three burnt, two of them together at the start

    [0, 0, 7, 3, 0, 8] → [7, 3, 8, 0, 0, 0]

  • Nothing burnt

    [5, 14, 2] → [5, 14, 2]

  • Everything burnt

    [0, 0, 0] → [0, 0, 0]

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def arrange_tray(tray):
    pass

JavaScript

function arrangeTray(tray) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →