Write a function named flip_half
that reverses the order of half of the elements of an array queue of elements
passed as a parameter. Your function should reverse the order of all the elements in odd-numbered positions (
position 1, 3, 5, etc.) assuming that the first value in the queue has position 0.
For example, if the array queue originally stores this sequence of numbers when the function is called:
index: 0 1 2 3 4 5 6 7
front [1, 8, 7, 2, 9, 18, 12, 0] back
Then it should store the following values after the function finishes executing:
index: 0 1 2 3 4 5 6 7
front [1, 0, 7, 18, 9, 2, 12, 8] back
Notice that numbers in even positions (positions 0, 2, 4, 6) have not moved.
That sub-sequence of numbers is still: (1
, 7
, 9
, 12
).
But notice that the numbers in odd positions (positions 1, 3, 5, 7) are now in reverse order relative to the original.
In other words, the original sub-sequence: (8
, 2
, 18
, 0
) -
has become: (0
, 18
, 2
, 8
).
Constraints: You may use a single stack array as auxiliary storage.
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) { ... }