Push operands
Updated
In postfix evaluation, numbers are operands: push them. Operators will later pop them. Do not apply anything at push time. Order on the stack is LIFO, so the most recent operand is the top (right operand of a binary op).
What to focus on
"2 3 +" : push 2, push 3, stack [2, 3] top 3. Tokens, not characters, if numbers can be multi-digit.
Worked examples
two numbers
s = "2 3"- push 2
- push 3
Result: [2, 3] · top 3
one
s = "7"- push 7
Result: [7]
three
s = "2 3 4"- push 2, 3, 4
Result: [2, 3, 4] · top 4
Try it
Write pushOperands(s). s is a space-separated sequence of integer operands. No operators in this drill. Tokenize and push each number. Do not add them yet. Top is the last number pushed. "2 3" → [2, 3] top 3. "7" → [7]. "2 3 4" → [2, 3, 4] top 4.
s is a space-separated sequence of integer operands. Length is at least 1. No operators in this drill.