logo CodeStepByStep logo

collection_mystery5

Write the output produced by the following function when passed each of the following queues. Assume that print_helper($arr) prints a collection in the of a comma-separated array surrounded by [] brackets, similar to that seen for each input. Note: A stack displays/prints in [bottom ... top] order, and a queue displays in [front ... back] order.

For example, if the state of $q and $s were [0, 1] and [2, 3], respectively, when printed in the given function, the expected answer would be:

q=[0, 1]
s=[2, 3]
        
function collection_mystery5($q) {
    $s = [];
    $size = count($q);

    for ($i = 0; $i < $size; $i++) {
        $n = array_shift($q);
        if ($n % 2 == 0) {
            array_push($s, $n);
        } else {
            array_shift($q, $n);
        }
    }

    print("q=" . print_helper($q));
    print("s=" . print_helper($s));
}
[1, 2, 3, 4, 5, 6]
[42, -3, 4, 15, 9, 71]
[30, 20, 10, 60, 50, 40, 3, 0]

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.