php please
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 (3)
//
function shiftNTimes(& $array, $numShifts) {
}
$arr = array (
1,
2,
3,
4 );
shiftNTimes ( $arr, 2 );
assert(3 == $arr[0]);
assert(4 == $arr[1]);
echo PHP_EOL . 'shiftNTimes(array(1, 2, 3, 4), 2)' . "\n";
// Use print_r to see all array elements on separate lines
print_r ( $arr );
?>
<?php
function shiftNTimes(& $array, $numShifts) {
$temp=[];//create a temp array
$len=sizeof($array);
for($i=0; $i<$numShifts; $i++)
{
$temp[$i]=$array[$i];
}
for($i=$numShifts; $i<$len; $i++)
{
$array[$i-$numShifts]=$array[$i];
}
for($i=0; $i<$numShifts; $i++)
{
$array[$len-$numShifts+$i]=$temp[$i];
}
print_r($array);
}
$arr = array (
1,
2,
3,
4
);
assert(3 == $arr[0]);
assert(4 == $arr[1]);
$a = readline('Enter the digit upto which you want to leftshift:
');
echo $a;
echo PHP_EOL . 'shiftNTimes(array(1, 2, 3, 4),'. $a.' )' . "\n";
// Use print_r to see all array elements on separate lines
print_r ( $arr );
shiftNTimes ( $arr, $a );
?>
//fell free to ask any questions
Get Answers For Free
Most questions answered within 1 hours.