Skip to content

Instantly share code, notes, and snippets.

@k0ff33
Last active January 6, 2024 17:19
Show Gist options
  • Save k0ff33/3029d30fb4ae62a72b0c54f2cb39893d to your computer and use it in GitHub Desktop.
Save k0ff33/3029d30fb4ae62a72b0c54f2cb39893d to your computer and use it in GitHub Desktop.
Cyclic Rotation Exercise from Codility (JavaScript/NodeJS)
/**
* Shift items in the array to the right by number of indexes
* @param {array} A
* @param {int} K
* @return {array}
*/
function solution(A, K) {
// if A is meant to be shifted by its own length (K) then just return the array
if (A.length === K || K === 0) {
return A
}
// Run K number of times saving last element in the array as a temporary variable, adding it to the front of the array and removing the last element
for (let i = 0; i < K; i++) {
let lastElement = A[A.length - 1]
A.unshift(lastElement)
A.pop()
}
return A
}
@bumsyalao
Copy link

bumsyalao commented Jul 11, 2019

`function solution(A, K) {
let newArr = [];
newArr = A.splice(K).concat(A.slice(0,K);
return newArr;
}

solution([1,2,3,4], 3);
`

@amannn
Copy link

amannn commented Dec 21, 2019

I've got 100% with this one:

function solution(A, K) {
  K = K % A.length;
  
  if (K === 0 || K === A.length) {
      return A;
  }
  
  const sliceIndex = A.length - K;
  return [...A.slice(sliceIndex), ...A.slice(0, sliceIndex)]
}

@Jorybraun
Copy link

Jorybraun commented Jul 19, 2020

You don't need the conditional early return to score 100%

function solution(A, K) {
    K = K % A.length;
    const sliceIndex = A.length - K;
    return [...A.slice(sliceIndex), ...A.slice(0, sliceIndex)]
 }

@saladinjake
Copy link

function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
//given: A
const given = A // of N integers
const givenTimes =K;

//conditions: last index  is moved to the first place

// let lastItemIndex = given.length -1;
//let lastItem = given[];
let firstPlace = given[0];
//task: rotate to the right each index k times

//ktimes means a loop the hell out of k possible out comes
for(let i =0; i <givenTimes; i++){
   //rotate to the right each index k times means another loop the hell out of the given array in question
   let popOutTheLastOne = given.pop();
   //then put it in the first
   given.unshift(popOutTheLastOne)
   
}

return given

}

@andrepadez
Copy link

andrepadez commented Jan 7, 2021

function solution(A, K) {
    for (let i = 0, k = K % A.length || 0; i < k; i++) {
        A.unshift(A.pop())
    }
    return A
}

@yamankatby
Copy link

My solution

function solution(A, K) {
  if (A.length === 0) return A;
  for (let i = 0; i < K; i++) {
    A.unshift(A.pop());
  }
  return A;
}

@elendil7
Copy link

elendil7 commented Feb 6, 2022

My solution

function solution(A, K) {
  // edge case destroyer
  if (A.length === 0 || A.length === K || K === 0) return A;
  let finalArr = A;
  // add characters to the start of finalArr
  for (let i = 0; i < K; i++) {
  finalArr.unshift(A[A.length - 1 - i]);
  }
  // assign sliced array to variable
  finalArr = finalArr.slice(0, -K);
  console.log(finalArr);
  // return sliced array
  return finalArr;
}

@CristhianSuriel
Copy link

This is my solution for 100%

function solution(A, K) {
    const size = A.length // sets length
    if(K>size) K = (K - (size*(Math.floor(K/size)))) // simplifies rotation (also realized you could do k = k % size)
    if(size === 0) return []; // if the string is empty return it as is
    
    const insertLeft = A.slice(-1 * K)
    const rotateRight = A.slice(0, -1 * K)
    const result = insertLeft.concat(rotateRight)
    return result
}

@leqnam
Copy link

leqnam commented Jan 6, 2024

My solution

function solution(A, K) {
  // edge case destroyer
  if (A.length === 0 || A.length === K || K === 0) return A;
  let finalArr = A;
  // add characters to the start of finalArr
  for (let i = 0; i < K; i++) {
  finalArr.unshift(A[A.length - 1 - i]);
  }
  // assign sliced array to variable
  finalArr = finalArr.slice(0, -K);
  console.log(finalArr);
  // return sliced array
  return finalArr;
}

Solution from elendil7 fail performance tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment