anagreg

Rotate list

Jun 1st, 2023
191
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 - 1) {
  18.   curr = list;
  19.   curr.push(list[0]);
  20.   curr.shift(list[0]);
  21. } else {
  22.   for (let i = N; i < list.length; i++) {
  23.     curr[k] = list[i];
  24.     k++;
  25.   }
  26.  
  27.   for (let i = 0; i < N; i++) {
  28.     curr[k] = list[i];
  29.     k++;
  30.   }
  31. }
  32. for (let j = 0; j < curr.length; j++) {
  33.   sum = `${sum}${curr[j]},`;
  34. }
  35.  
  36. print(sum.substring(0, sum.length - 1));
Advertisement
Add Comment
Please, Sign In to add comment