logo CodeStepByStep logo

collection_mystery8

Write the result returned by the following function when passed each of the following parameters. Your answer should be in the same notation as the input collection (a comma-separated array surrounded by [] brackets). Note: A stack displays/prints in [bottom ... top] order, and a queue displays in [front ... back] order.

function collection_mystery8($stack, $n) {
    $stack2 = [];
    $queue = [];

    while (count($stack) > $n) {
        array_push($queue, array_pop($stack));
    }
    while (count($stack) > 0) {
        $element = array_pop($stack);
        array_push($stack2, $element);
        if ($element % 2 == 0) {
            array_push($queue, $element);
        }
    }
    while (count($queue) > 0) {
        array_push($stack, array_shift($queue));
    }
    while (count($stack2) > 0) {
        array_push($stack, array_pop($stack2));
    }

    return $stack;

}
[1, 3, 5], n=1
[0, -2, -4], n=1
[0, 2, 4], n=2
[1, 2, 3, 4, 5, 6], n=3
[67, 29, 115, 84, 33, 71, 90], n=5

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.