Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#Printing a list and clearing the stack

Printing a list and clearing the stack

Lets say your stack has a list of strings/numbers/etc. on top and some other extra items below it. i.e.

123 "waste" ["a" "b" "rty" "print" "me" "please"]

Now you are interested in printing the last list only, so you do

S*]W=

which outputs

a b rty print me please

Which seems really smart as we use the clearing the stack trick and only print the list joined with spaces (which might not be the desired way of printing a list at times).

This can be golfed further!

p];

That's 2 bytes shorter!

and if you have only 1 item on stack other than the list, its even shorter!

p;

The beauty of p is that it removes the top most item from stack, stringifies it (also adds a newline at the end) and prints to STDOUT instantly, without waiting for completion of the code.

So the above code will output

["a" "b" "rty" "print" "me" "please"]

which is the exact representation of a list when it was in stack!

#Printing a list and clearing the stack

Lets say your stack has a list of strings/numbers/etc. on top and some other extra items below it. i.e.

123 "waste" ["a" "b" "rty" "print" "me" "please"]

Now you are interested in printing the last list only, so you do

S*]W=

which outputs

a b rty print me please

Which seems really smart as we use the clearing the stack trick and only print the list joined with spaces (which might not be the desired way of printing a list at times).

This can be golfed further!

p];

That's 2 bytes shorter!

and if you have only 1 item on stack other than the list, its even shorter!

p;

The beauty of p is that it removes the top most item from stack, stringifies it (also adds a newline at the end) and prints to STDOUT instantly, without waiting for completion of the code.

So the above code will output

["a" "b" "rty" "print" "me" "please"]

which is the exact representation of a list when it was in stack!

Printing a list and clearing the stack

Lets say your stack has a list of strings/numbers/etc. on top and some other extra items below it. i.e.

123 "waste" ["a" "b" "rty" "print" "me" "please"]

Now you are interested in printing the last list only, so you do

S*]W=

which outputs

a b rty print me please

Which seems really smart as we use the clearing the stack trick and only print the list joined with spaces (which might not be the desired way of printing a list at times).

This can be golfed further!

p];

That's 2 bytes shorter!

and if you have only 1 item on stack other than the list, its even shorter!

p;

The beauty of p is that it removes the top most item from stack, stringifies it (also adds a newline at the end) and prints to STDOUT instantly, without waiting for completion of the code.

So the above code will output

["a" "b" "rty" "print" "me" "please"]

which is the exact representation of a list when it was in stack!

Mod Removes Wiki by Doorknob
Source Link
Optimizer
  • 26.6k
  • 7
  • 67
  • 144

#Printing a list and clearing the stack

Lets say your stack has a list of strings/numbers/etc. on top and some other extra items below it. i.e.

123 "waste" ["a" "b" "rty" "print" "me" "please"]

Now you are interested in printing the last list only, so you do

S*]W=

which outputs

a b rty print me please

Which seems really smart as we use the clearing the stack trick and only print the list joined with spaces (which might not be the desired way of printing a list at times).

This can be golfed further!

p];

That's 2 bytes shorter!

and if you have only 1 item on stack other than the list, its even shorter!

p;

The beauty of p is that it removes the top most item from stack, stringifies it (also adds a newline at the end) and prints to STDOUT instantly, without waiting for completion of the code.

So the above code will output

["a" "b" "rty" "print" "me" "please"]

which is the exact representation of a list when it was in stack!

AltStyle によって変換されたページ (->オリジナル) /