Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation Valid operators: +, -, *, / Division truncates toward zero Example: ["2","1","+","3","*"] => 9 ((2+1)*3)
I push numbers on a stack. Each operator pops two, applies itself and pushes the result. Operand order matters for minus and divide.
Examples
You write
eval_rpn(tokens: List[str])
Example 1
- Input
- tokens = ["2","1","+","3","*"]
- returns
- 9
Example 2
- Input
- tokens = ["4","13","5","/","+"]
- returns
- 6
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsStack
I reach for it when
- Things must close in the reverse order they opened.
- You need the nearest earlier or later item that is bigger or smaller.
Say this before you type. Say what each item on the stack is waiting for, and what lets you pop it.
All 17 Stack problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.