Skip to content

Instantly share code, notes, and snippets.

@CoGrammarCodeReview
Last active August 2, 2022 12:37
Show Gist options
  • Save CoGrammarCodeReview/17a2e3c5610d2d20ca8e4d88b0bd5e89 to your computer and use it in GitHub Desktop.
Save CoGrammarCodeReview/17a2e3c5610d2d20ca8e4d88b0bd5e89 to your computer and use it in GitHub Desktop.
Stack challenge
A stack machine processes instructions by pushing and popping values to an internal stack. A simple example of this is a calculator.
The argument passed to run(instructions) will always be a string containing a series of instructions.
The instruction set of the calculator will be this:
+: Pop the last 2 values from the stack, add them, and push the result onto the stack.
-: Pop the last 2 values from the stack, subtract the lower one from the topmost one, and push the result.
*: Pop the last 2 values, multiply, and push the result.
/: Pop the last 2 values, divide the topmost one by the lower one, and push the result.
DUP: Duplicate (not double) the top value on the stack.
POP: Pop the last value from the stack and discard it.
PSH: Performed whenever a number appears as an instruction. Push the number to the stack.
Any other instruction (for example, a letter) should result in the value "Invalid instruction: [instruction]"
Examples
StackCalc("") ➞ 0
StackCalc("5 6 +") ➞ 11
StackCalc("3 DUP +") ➞ 6
StackCalc("6 5 5 7 * - /") ➞ 5
StackCalc("x y +") ➞ Invalid instruction: x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment