4
28/10/2024 6:30 pm
Début du sujet
Write a program that receives an array and number of rotations you have to perform (first element goes at the end).
Print the resulting array.
Examples:

2 Réponses
3
28/10/2024 6:31 pm
My answer:
<?php
$array = array_map('intval', explode(' ', readline()));
$rotations = intval(readline());
$count = count($array);
$rotations = $rotations % $count;
$newArray = [];
for ($i = $rotations; $i < $count; $i++) {
$newArray[] = $array[$i];
}
for ($i = 0; $i < $rotations; $i++) {
$newArray[] = $array[$i];
}
echo implode(' ', $newArray);
2
28/10/2024 6:33 pm
another solution with array_shift():
<?php
$array = array_map('intval', explode(' ', readline()));
$rotations = intval(readline());
for ($i = 0; $i < $rotations; $i++) {
$first = array_shift($array);
$array[] = $first;
}
echo implode(' ', $array);
