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%~
But this is GolfScript, surely it can be done with fewer characters?
4 Answers 4
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
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
-
2\$\begingroup\$ 19 bytes:
~:k;]:!k<!k>(:x;+~xTry it online! \$\endgroup\$noodle person– noodle person2024年07月23日 22:35:13 +00:00Commented Jul 23, 2024 at 22:35
17 bytes
(:x{[\]}*{~@@}x*\
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.
(削除) 16 (削除ここまで) (削除) 15 (削除ここまで) 14 bytes
~]:a)<~a)>{\}*
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.
-
\$\begingroup\$ You don't need the
avariable:~].)<\)>(:x;+~x\$\endgroup\$ovs– ovs2024年07月23日 22:58:43 +00:00Commented Jul 23, 2024 at 22:58 -
\$\begingroup\$ Nice catch, @ovs, I think that crossed my mind but I didn't think it would be shorter. \$\endgroup\$noodle person– noodle person2024年07月23日 23:32:14 +00:00Commented Jul 23, 2024 at 23:32
-
1\$\begingroup\$ 14 bytes:
~]:a)<~a)>{\}*\$\endgroup\$jimmy23013– jimmy230132024年07月24日 09:55:17 +00:00Commented Jul 24, 2024 at 9:55 -
\$\begingroup\$ @jimmy23013 Wow, great idea to do
{\}*, I don't think I could've thought of that. \$\endgroup\$noodle person– noodle person2024年07月24日 13:08:03 +00:00Commented Jul 24, 2024 at 13:08
(削除) 22 (削除ここまで) 19 bytes
])~11ドル$<@@>[(](\++~
explanation:
]) # Separate the stack and the argument
~ # NOT the argument
11ドル$ # 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