Skip to content

Instantly share code, notes, and snippets.

@rndD
Created May 3, 2021 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rndD/5f9880a7710c904eee8a6aac67194e10 to your computer and use it in GitHub Desktop.
Save rndD/5f9880a7710c904eee8a6aac67194e10 to your computer and use it in GitHub Desktop.
func pop(alist *[]int) int {
f := len(*alist)
rv := (*alist)[f-1]
*alist = append((*alist)[:f-1])
return rv
}
func calc(b int, a int, f string) int {
if f == "/" {
return a / b
}
if f == "+" {
return a + b
}
if f == "-" {
return a - b
}
if f == "*" {
return a * b
}
return 0
}
func evalRPN(tokens []string) int {
st := make([]int, 0)
for _, j := range tokens {
jj, err := strconv.Atoi(j)
if err != nil {
a := pop(&st)
b := pop(&st)
st = append(st, calc(a, b, j))
} else {
st = append(st, jj)
}
}
return st[0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment