Advertisement
CR7CR7

rotate-list

Jun 1st, 2023
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let input = ["1,2,3", "2"];
  2.  
  3. let print = this.print || console.log;
  4. let gets =
  5.   this.gets ||
  6.   (
  7.     (arr, index) => () =>
  8.       arr[index++]
  9.   )(input, 0);
  10.  
  11. let list = gets().split(",").map(Number);
  12. let N = +gets();
  13. let curr = [];
  14. let k = 0;
  15. let sum = "";
  16.  
  17. if (N > list.length) {
  18.   N = N % list.length; // Corrected line: Adjust N if it's larger than the length of the list
  19. }
  20.  
  21. for (let i = N; i < list.length; i++) {
  22.   curr[k] = list[i];
  23.   k++;
  24. }
  25.  
  26. for (let i = 0; i < N; i++) {
  27.   curr[k] = list[i];
  28.   k++;
  29. }
  30.  
  31. for (let j = 0; j < curr.length; j++) {
  32.   sum = `${sum}${curr[j]},`;
  33. }
  34.  
  35. print(sum.substring(0, sum.length - 1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement