Codility cyclic rotation solution in PHP
up vote
1
down vote
favorite
Problem statement
A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.
For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.
My Solution
function solution($A, $K) {
// when array is empty or has only one element
if(count($A) == 0 || count($A) == 1){
return $A;
}
//runs k times
for($j=1; $j<=$K; $j++){
$last_element = $A[count($A)-1];
//runs for each element
for($i=(count($A)-1); $i>0; $i--){
$A[$i] = $A[$i-1];
}
$A[0] = $last_element;
}
return $A;
}
$A = [1, 2, 3, 4];
$K = 4;
$result = solution($A, $K);
print_r($result);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
php programming-challenge array
add a comment |
up vote
1
down vote
favorite
Problem statement
A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.
For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.
My Solution
function solution($A, $K) {
// when array is empty or has only one element
if(count($A) == 0 || count($A) == 1){
return $A;
}
//runs k times
for($j=1; $j<=$K; $j++){
$last_element = $A[count($A)-1];
//runs for each element
for($i=(count($A)-1); $i>0; $i--){
$A[$i] = $A[$i-1];
}
$A[0] = $last_element;
}
return $A;
}
$A = [1, 2, 3, 4];
$K = 4;
$result = solution($A, $K);
print_r($result);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
php programming-challenge array
How about rotating the 4-element A array K=4 million times...?
– CiaPan
Mar 16 at 8:07
Welcome de CodeReview.SE! Please add a link to the original problem in your question.
– Josay
Mar 16 at 16:54
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Problem statement
A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.
For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.
My Solution
function solution($A, $K) {
// when array is empty or has only one element
if(count($A) == 0 || count($A) == 1){
return $A;
}
//runs k times
for($j=1; $j<=$K; $j++){
$last_element = $A[count($A)-1];
//runs for each element
for($i=(count($A)-1); $i>0; $i--){
$A[$i] = $A[$i-1];
}
$A[0] = $last_element;
}
return $A;
}
$A = [1, 2, 3, 4];
$K = 4;
$result = solution($A, $K);
print_r($result);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
php programming-challenge array
Problem statement
A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.
For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.
My Solution
function solution($A, $K) {
// when array is empty or has only one element
if(count($A) == 0 || count($A) == 1){
return $A;
}
//runs k times
for($j=1; $j<=$K; $j++){
$last_element = $A[count($A)-1];
//runs for each element
for($i=(count($A)-1); $i>0; $i--){
$A[$i] = $A[$i-1];
}
$A[0] = $last_element;
}
return $A;
}
$A = [1, 2, 3, 4];
$K = 4;
$result = solution($A, $K);
print_r($result);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
php programming-challenge array
php programming-challenge array
edited Mar 17 at 17:47
200_success
127k15149412
127k15149412
asked Mar 16 at 7:48
riser101
1133
1133
How about rotating the 4-element A array K=4 million times...?
– CiaPan
Mar 16 at 8:07
Welcome de CodeReview.SE! Please add a link to the original problem in your question.
– Josay
Mar 16 at 16:54
add a comment |
How about rotating the 4-element A array K=4 million times...?
– CiaPan
Mar 16 at 8:07
Welcome de CodeReview.SE! Please add a link to the original problem in your question.
– Josay
Mar 16 at 16:54
How about rotating the 4-element A array K=4 million times...?
– CiaPan
Mar 16 at 8:07
How about rotating the 4-element A array K=4 million times...?
– CiaPan
Mar 16 at 8:07
Welcome de CodeReview.SE! Please add a link to the original problem in your question.
– Josay
Mar 16 at 16:54
Welcome de CodeReview.SE! Please add a link to the original problem in your question.
– Josay
Mar 16 at 16:54
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
You could use array functions. They simplify and speed up the handling of arrays. See: http://php.net/manual/en/ref.array.php
So your code could become:
function rotateArray($inputArray,$rightShiftCount)
// shift all elements of the array to the right a number of times
{
// extract the part of the array to move to the front
$partToMove = array_splice($inputArray,-($rightShiftCount % count($inputArray)));
// return extract part followed by what was left of the array
return array_merge($partToMove,$inputArray);
}
This function does not check its arguments, if needed you can add that.
Notice that I have used sensible names for my variables instead of $A
and $K
. This is intentional.
add a comment |
up vote
0
down vote
Actually, you don't need to rotate the initial array K
times:
<?php
function solution($A, $K)
{
// when array is empty or has only one element
if (count($A) == 0 || count($A) == 1) {
return $A;
}
// The number of rotations needed
$rotateTimes = $K % count($A);
//runs `$rotateTimes` times
for ($j = 1; $j <= $rotateTimes; $j++) {
$last_element = $A[count($A) - 1];
//runs for each element
for ($i = (count($A) - 1); $i > 0; $i--) {
$A[$i] = $A[$i - 1];
}
$A[0] = $last_element;
}
return $A;
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You could use array functions. They simplify and speed up the handling of arrays. See: http://php.net/manual/en/ref.array.php
So your code could become:
function rotateArray($inputArray,$rightShiftCount)
// shift all elements of the array to the right a number of times
{
// extract the part of the array to move to the front
$partToMove = array_splice($inputArray,-($rightShiftCount % count($inputArray)));
// return extract part followed by what was left of the array
return array_merge($partToMove,$inputArray);
}
This function does not check its arguments, if needed you can add that.
Notice that I have used sensible names for my variables instead of $A
and $K
. This is intentional.
add a comment |
up vote
1
down vote
You could use array functions. They simplify and speed up the handling of arrays. See: http://php.net/manual/en/ref.array.php
So your code could become:
function rotateArray($inputArray,$rightShiftCount)
// shift all elements of the array to the right a number of times
{
// extract the part of the array to move to the front
$partToMove = array_splice($inputArray,-($rightShiftCount % count($inputArray)));
// return extract part followed by what was left of the array
return array_merge($partToMove,$inputArray);
}
This function does not check its arguments, if needed you can add that.
Notice that I have used sensible names for my variables instead of $A
and $K
. This is intentional.
add a comment |
up vote
1
down vote
up vote
1
down vote
You could use array functions. They simplify and speed up the handling of arrays. See: http://php.net/manual/en/ref.array.php
So your code could become:
function rotateArray($inputArray,$rightShiftCount)
// shift all elements of the array to the right a number of times
{
// extract the part of the array to move to the front
$partToMove = array_splice($inputArray,-($rightShiftCount % count($inputArray)));
// return extract part followed by what was left of the array
return array_merge($partToMove,$inputArray);
}
This function does not check its arguments, if needed you can add that.
Notice that I have used sensible names for my variables instead of $A
and $K
. This is intentional.
You could use array functions. They simplify and speed up the handling of arrays. See: http://php.net/manual/en/ref.array.php
So your code could become:
function rotateArray($inputArray,$rightShiftCount)
// shift all elements of the array to the right a number of times
{
// extract the part of the array to move to the front
$partToMove = array_splice($inputArray,-($rightShiftCount % count($inputArray)));
// return extract part followed by what was left of the array
return array_merge($partToMove,$inputArray);
}
This function does not check its arguments, if needed you can add that.
Notice that I have used sensible names for my variables instead of $A
and $K
. This is intentional.
answered Mar 17 at 16:23
KIKO Software
1,549512
1,549512
add a comment |
add a comment |
up vote
0
down vote
Actually, you don't need to rotate the initial array K
times:
<?php
function solution($A, $K)
{
// when array is empty or has only one element
if (count($A) == 0 || count($A) == 1) {
return $A;
}
// The number of rotations needed
$rotateTimes = $K % count($A);
//runs `$rotateTimes` times
for ($j = 1; $j <= $rotateTimes; $j++) {
$last_element = $A[count($A) - 1];
//runs for each element
for ($i = (count($A) - 1); $i > 0; $i--) {
$A[$i] = $A[$i - 1];
}
$A[0] = $last_element;
}
return $A;
}
add a comment |
up vote
0
down vote
Actually, you don't need to rotate the initial array K
times:
<?php
function solution($A, $K)
{
// when array is empty or has only one element
if (count($A) == 0 || count($A) == 1) {
return $A;
}
// The number of rotations needed
$rotateTimes = $K % count($A);
//runs `$rotateTimes` times
for ($j = 1; $j <= $rotateTimes; $j++) {
$last_element = $A[count($A) - 1];
//runs for each element
for ($i = (count($A) - 1); $i > 0; $i--) {
$A[$i] = $A[$i - 1];
}
$A[0] = $last_element;
}
return $A;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Actually, you don't need to rotate the initial array K
times:
<?php
function solution($A, $K)
{
// when array is empty or has only one element
if (count($A) == 0 || count($A) == 1) {
return $A;
}
// The number of rotations needed
$rotateTimes = $K % count($A);
//runs `$rotateTimes` times
for ($j = 1; $j <= $rotateTimes; $j++) {
$last_element = $A[count($A) - 1];
//runs for each element
for ($i = (count($A) - 1); $i > 0; $i--) {
$A[$i] = $A[$i - 1];
}
$A[0] = $last_element;
}
return $A;
}
Actually, you don't need to rotate the initial array K
times:
<?php
function solution($A, $K)
{
// when array is empty or has only one element
if (count($A) == 0 || count($A) == 1) {
return $A;
}
// The number of rotations needed
$rotateTimes = $K % count($A);
//runs `$rotateTimes` times
for ($j = 1; $j <= $rotateTimes; $j++) {
$last_element = $A[count($A) - 1];
//runs for each element
for ($i = (count($A) - 1); $i > 0; $i--) {
$A[$i] = $A[$i - 1];
}
$A[0] = $last_element;
}
return $A;
}
answered Mar 17 at 12:59
Joe Black
1663
1663
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189734%2fcodility-cyclic-rotation-solution-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How about rotating the 4-element A array K=4 million times...?
– CiaPan
Mar 16 at 8:07
Welcome de CodeReview.SE! Please add a link to the original problem in your question.
– Josay
Mar 16 at 16:54