Ping Pong 300

By Uday Shankar

This is the classic problem of using two stacks to emulate a queue. You can find pseudocode for it on google, but here we give a brief description of how to implement the two queue operations involved.

  • Enqueue - push the number onto stack 1

  • Dequeue

    • If stack 2 has something on it, pop from stack 2 and return

    • If stack 2 is empty, pop from stack 1 and push to stack 2 until stack 1 is empty. This reverses the order of elements from LIFO (stack) to FIFO (queue). Then, pop from stack 2 and return.

The Ping Pong solution is almost identical to this. Stack 1 is the normal stack, and Stack 2 is the Helper Stack ®. The only quirk we must worry about is that only elements on the top of stack 1 can be directly manipulated by I/O, arithmetic, and flow commands. Therefore, sometimes we must move elements from the top of stack 2 to the top of stack 1 to perform some operation. A solution is given below, courtesy Jakob Degen.

\        @
\01-#/;<#/=\!#/<\':N6\
              \`/'\  *
     \     /#  !\=/#./

Flag: PP_wow_we_totally_didnt_give_you_the_flag

Note: The flag changed during the competition, because we accidentally distributed the original flag in the ping pong interpreter that we provided. Oops.

results matching ""

    No results matching ""