logo CodeStepByStep logo

split_stack

Language/Type: PHP Stack Queue collections

Write a function named split_stack that takes as a parameter an array stack of numbers, and re-orders it so that all the non-negative numbers are at the top in the reverse of their original relative order, and all the negative numbers are at the bottom in the reverse of their original relative order.

For example, if passed the stack [4, 0, -1, 5, -6, -3, 2, 7], your function should modify the stack to store [-3, -6, -1, 7, 2, 5, 0, 4].

Constraints: Do not declare any auxiliary data structures other than a single temporary array queue.

A note about references in PHP: In order to write a function passes a parameter as reference (thus modifying its state), you'll need to prepend "&" to the variable declaration in the function header. For example, a function foo that modifies the state of an array parameter may be defined as:

function foo(&$arr) { ... }
Function: Write a PHP function as described, not a complete program.

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.