# GolfScript many-items rotation

There isn't a builtin in golfscript which lets you rotate a bigger portion of the stack than the three top-most elements.

It should take one number n as argument and rotate the nth value from the top, to the top, so a b c d e f 4 becomes a c d e f b, and it should work regardless of the types of the items.

I came up with this in 27 bytes:

]-1%({1$}4*<@@)>+@@=\~]-1%~  Try it online! But this is GolfScript, surely it can be done with fewer characters? ## 4 Answers # 24 bytes there is still plenty of room to golf this further, but I think this version shows the idea well. :k;]:s;s k~<s k~>(:x;+~x  Try it online! with some comments: :k;]:s; # inputs: number k, rest of stack s s k~< # stack without top k+1 values s k~> # top k+1 values (:x; # the value to be rotated to the front +~x # push everything to the stack  • 19 bytes: ~:k;]:!k<!k>(:x;+~x Try it online! Commented Jul 23 at 22:35 # 17 bytes (:x{[\]}*{~@@}x*\  Try it online! An entirely different approach that does not involve wrapping entire stack. Given n, the two top values are wrapped n-1 times, putting the item to be brought up at the 2nd top position. Then the nested pairs are unwrapped, keeping the target at the 2nd top position, and finally it gets swapped to the top. # 1615 14 bytes ~]:a)<~a)>{\}*  Try it online! Based on ovs's solution, but instead of separating N from the stack and assigning each to a variable, I do the ~ before collecting the stack into an array, and just use this array with ). Then the first part of the stack is dumped, and the second part is dumped by doing {\}* (reduce by swapping) which was jimmy23013's great idea to dump while moving the front of the array to the back. ovs had a suggestion that reduced this from 16 to 15 bytes by eliminating the variable a. However jimmy23013 later suggested to add that variable a back in and rather eliminate the variable x, which is even shorter! Thanks to both for their suggestions. • You don't need the a variable: ~].)<\)>(:x;+~x – ovs Commented Jul 23 at 22:58 • Nice catch, @ovs, I think that crossed my mind but I didn't think it would be shorter. Commented Jul 23 at 23:32 • 14 bytes: ~]:a)<~a)>{\}* Commented Jul 24 at 9:55 • @jimmy23013 Wow, great idea to do {\}*, I don't think I could've thought of that. Commented Jul 24 at 13:08 # 22 19 bytes ])~1$1$<@@>[(](\++~  Try it online! explanation: ]) # Separate the stack and the argument ~ # NOT the argument 1$1\$   # Duplicate the stack and argument so there are two of each, alternating
<      # Objects under the index
@@>    # Objects at or above the index
[(](\  # Move the object at the index to the top, in a list
++~    # Concatenate and unpack everything