logo CodeStepByStep logo

collection_mystery6

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_mystery6($queue, $n) {
    $stack = [];
    $count = 0;
    $size = count($queue);
    for ($i = 0; $i < $size; $i++) {
        $element = array_shift($queue);
        if ($element < $n) {
            array_push($queue, $element);
        } else {
            $count++;
            array_push($stack, $count);
            array_push($stack, $element);
        }
    }
    
    while (count($stack) > 0) {
        array_push($queue, array_pop($stack));
    }
    return $queue;  
}
[1, 2, 3, 4, 5], n=4
[-5, 4, -3, 2, -1, 0], n=0
[67, 29, 115, 84, 33, 71, 90], n=50

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.