(tokens []string)
| 1 | func evalRPN(tokens []string) int { |
| 2 | var stack []int |
| 3 | var a, b int |
| 4 | for _, c := range tokens { |
| 5 | switch c { |
| 6 | case "+": |
| 7 | a, b, stack = getAndPopLastOperand(stack) |
| 8 | stack = append(stack, (a + b)) |
| 9 | case "-": |
| 10 | a, b, stack = getAndPopLastOperand(stack) |
| 11 | stack = append(stack, (a - b)) |
| 12 | case "*": |
| 13 | a, b, stack = getAndPopLastOperand(stack) |
| 14 | stack = append(stack, (a * b)) |
| 15 | case "/": |
| 16 | a, b, stack = getAndPopLastOperand(stack) |
| 17 | stack = append(stack, (a / b)) |
| 18 | default: |
| 19 | i, _ := strconv.Atoi(c) |
| 20 | stack = append(stack, i) |
| 21 | } |
| 22 | } |
| 23 | return stack[0] |
| 24 | } |
| 25 | |
| 26 | func getAndPopLastOperand(stack []int) (int, int, []int) { |
| 27 | a := stack[len(stack)-2] |
nothing calls this directly
no test coverage detected