logo CodeStepByStep logo

collection_mystery7

Write the result returned by the following function when passed each of the following stacks. 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_mystery7($s) {
    $q = [];
    $s2 = [];

    while (count($s) > 0) {
        $n = array_pop($s);
        if ($n % 2 == 0) {
            array_push($q, $n);
        } else {
            array_push($s2, $n);
        }
    }

    while (count($q) > 0) {
        array_push($s, array_shift($q));
    }
    while (count($s2) > 0) {
        array_push($s, array_pop($s2));
    }

    return $s;
}
[1, 2, 3, 4, 5, 6]
[42, 3, 12, 15, 9, 71, 88]
[65, 30, 10, 20, 45, 55, 6, 1]

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.