10
\$\begingroup\$

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?

noodle person
12.6k1 gold badge31 silver badges90 bronze badges
asked Jul 23, 2024 at 11:46
\$\endgroup\$

4 Answers 4

4
\$\begingroup\$

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
answered Jul 23, 2024 at 15:38
\$\endgroup\$
1
  • 2
    \$\begingroup\$ 19 bytes: ~:k;]:!k<!k>(:x;+~x Try it online! \$\endgroup\$ Commented Jul 23, 2024 at 22:35
4
\$\begingroup\$

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.

answered Jul 24, 2024 at 9:59
\$\endgroup\$
4
\$\begingroup\$

(削除) 16 (削除ここまで) (削除) 15 (削除ここまで) 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.

answered Jul 23, 2024 at 21:32
\$\endgroup\$
4
  • \$\begingroup\$ You don't need the a variable: ~].)<\)>(:x;+~x \$\endgroup\$ Commented 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\$ Commented Jul 23, 2024 at 23:32
  • 1
    \$\begingroup\$ 14 bytes: ~]:a)<~a)>{\}* \$\endgroup\$ Commented Jul 24, 2024 at 9:55
  • \$\begingroup\$ @jimmy23013 Wow, great idea to do {\}*, I don't think I could've thought of that. \$\endgroup\$ Commented Jul 24, 2024 at 13:08
3
\$\begingroup\$

(削除) 22 (削除ここまで) 19 bytes

])~11ドル$<@@>[(](\++~

Try it online!

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
answered Jul 23, 2024 at 16:37
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.