Question
//////////////////////////////////////////////////////////////////////////////// // function shiftNTimes // // Modify array so it is left shifted n times -- so shiftNTimes(array(6, 2, 5, 3), 1) // changes the
//////////////////////////////////////////////////////////////////////////////// // function shiftNTimes // // Modify array so it is "left shifted" n times -- so shiftNTimes(array(6, 2, 5, 3), 1) // changes the array argument to (2, 5, 3, 6) and shiftNTimes(array(6, 2, 5, 3), 2) // changes the array argument to (5, 3, 6, 2). You must modify the array argument by // changing the parameter array inside method shiftNTimes. A change to the // parameter inside the method shiftNTimes changes the argument if the // argument is passed by reference, that means it is preceded by an ampersand & // // shiftNTimes( array(1, 2, 3, 4, 5, 6, 7), 3 ) modifies array to ( 4, 5, 6, 7, 1, 2, 3 ) // shiftNTimes( array(1, 2, 3), 5) modifies array to (3, 1, 2) // shiftNTimes( array(3), 5) modifies array to be the same (3) // // Feel free to have this function call a helper function // that shifts elements once, $numShifts times. // function shiftNTimes(& $array, $numShifts) { // No return! }
$array = array(1, 2, 3, 4, 5); shiftNTimes($array, 2); assert($array[0] == 3); assert($array[1] == 4); assert($array[2] == 5); assert($array[3] == 1); assert($array[4] == 2);
//////////////////////////////////////////////////////////////////////////// // function evenOdd(array) // // Modify array that contains the exact same numbers as the given array, but // rearranged so that all the even numbers come before all the odd numbers. // Other than that, the numbers can be in any order. // // evenOdd([1, 0, 1, 0, 0, 1, 1]) changes the array to [0, 0, 0, 1, 1, 1, 1] // evenOdd([3, 3, 2]) changes the array to [2, 3, 3] // evenOdd([2, 2, 2]) changes the array to [2, 2, 2] // // Precondition: All array elements are integers (whole numbers) function evenOdd(& $nums) { // No return! }
// function sumFromFileInput // // Return the sum of the numbers stored in $fileName. // // Precondition: the file stores valid numbers, one per line. // function sumFromFileInput($fileName) { return -999; }
// Test sumFromFileInput assert(sumFromFileInput('numbers.txt') === 54); PHP functions that do array processing. The last function reads from * an input file.
numbers.txt
1 1 2 3 5 8 13 21
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started