44
3
I can't believe we don't have this already.. It's one of the most important data-structures in programming, yet still simple enough to implement it in a code-golf:
Challenge
Your task is to implement a stack that allows pushing and popping numbers, to test your implementation and keep I/O simple we'll use the following setup:
- Input will be a list of non-negative integers
Every positive integer \$n\$ indicates a \$\texttt{push(}n\texttt{)}\$ and every \$0\$ indicates a \$\texttt{pop()}\$ - discarding the top element.
- Output will be the resulting stack
Example
For example if we're given \$[12,3,0,101,11,1,0,0,14,0,28]\$:
$$ \begin{aligned} & 12 & [12] \\ & 3 & [3,12] \\ & 0 & [12] \\ & 101 & [101,12] \\ & 11 & [11,101,12] \\ & 1 & [1,11,101,12] \\ & 0 & [11,101,12] \\ & 0 & [101,12] \\ & 14 & [14,101,12] \\ & 0 & [101,12] \\ & 28 & [28,101,12] \end{aligned} $$
Output will be: \$[28,101,12]\$
Rules
- Input will be a list of non-negative integers in any default I/O format
- you may use a negative integer to signify the end of a stream of integers
- Output will be a list/matrix/.. of the resulting stack
- your choice where the top element will be (at the beginning or end), the output just has to be consistent
- output is flexible (eg. integers separated by new-lines would be fine), the only thing that matters is the order
- you may use a negative integer to signify the bottom of the stack
- You're guaranteed that there will never be a \$0\$ when the stack is empty
Examples
[] -> []
[1] -> [1]
[1,0,2] -> [2]
[4,0,1,12] -> [12,1]
[8,3,1,2,3] -> [3,2,1,3,8]
[1,3,7,0,0,0] -> []
[13,0,13,10,1,0,1005,5,0,0,0] -> [13]
[12,3,0,101,11,1,0,0,14,0,28] -> [28,101,12]
12It should be noted that, given the conditions, one does not actually need to implement the stack. – Jeff Zeitlin – 2018-07-27T11:35:00.953
If you wanted someone to actually implement a stack, you might need to try putting something in the Sandbox. – mbomb007 – 2018-07-27T16:33:30.370
@mbomb007: Either is allowed: "your choice where the top element will be (at the beginning or end)" – ბიმო – 2018-07-27T16:49:32.337
@mbomb007: It wouldn't be any more difficult if you had to reverse the input, would it? Besides, if you consider the setup as a stack who defines what's the top and what's the bottom and why should one definition be less arbitrary? – ბიმო – 2018-07-27T16:57:45.767
@OMᗺ Because the input looks quite a bit like a stack/list/array. Now, the entire challenge is basically remove any number followed by a zero. – mbomb007 – 2018-07-27T18:10:38.220
@mbomb007: It's not though, what about
[1,2,0,0]
? – ბიმო – 2018-07-27T19:56:57.023Look at my answer in Retina. In pseudo-code:
– mbomb007 – 2018-07-27T20:23:48.207Loop: remove the first #,0
and you're done. So you remove2,0
, then remove1,0
.Let us continue this discussion in chat.
– ბიმო – 2018-07-27T20:25:49.363Related – xnor – 2018-07-27T21:40:24.557
We don't usually like to make different rules for different languages. Your spec says "if your language doesn't […] support […] lists, …" – would you consider opening this option to all languages? As @Titus pointed out, Python has perfectly good support for lists, yet you seem OK with a Python answer using negatives. – O.O.Balance – 2018-07-28T16:08:38.667
@O.O.Balance: I completely forgot that I restricted it, sorry about that.. (updated) – ბიმო – 2018-07-28T16:11:27.640
Is a terminating character allowed for the output? For the given example, is
[28, 101, 12, 0]
allowed? – Jon Claus – 2018-07-31T20:39:57.590@JonClaus: Why did you change your question after I allowed it (something else)? That's not cool! I'll allow a negative integer marking the bottom of the stack, as was originally asked for since it's also allowed to mark end of input. Also, welcome to PPCG! – ბიმო – 2018-07-31T20:59:04.283
@OMᗺ Sorry about the switch from -1 to 0. I figured both were equivalent for the purposes of an answer because neither is a possible value in the stack, which can only contain positive integers. – Jon Claus – 2018-07-31T21:03:14.377