Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CollinShoop/fa36a11d5a38cd9f1637b2749f4ffb9e to your computer and use it in GitHub Desktop.
Save CollinShoop/fa36a11d5a38cd9f1637b2749f4ffb9e to your computer and use it in GitHub Desktop.
func evalRPN(tokens []string) int {
nums := []int{}
push := func(n int) {
nums = append(nums, n)
}
pop := func() int {
n := nums[len(nums)-1]
nums = nums[:len(nums)-1]
return n
}
for _, token := range tokens {
switch token {
case "*": push(pop() * pop())
case "/":
tmp := pop();
push(pop() / tmp)
case "-":
tmp := pop();
push(pop() - tmp)
case "+": push(pop() + pop())
default:
n, _ := strconv.Atoi(token)
push(n)
}
}
return pop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment