Untitled
unknown
plain_text
a year ago
815 B
17
Indexable
import (
"fmt"
"strconv"
)
func getResult(x , y int , operator string) int {
switch operator {
case "+" : return x + y
case "-" : return x - y
case "*" : return x * y
case "/" : return x / y
}
return 0
}
func evalRPN(tokens []string) int {
answer := []int{}
for _ , str := range tokens {
if str == "+" || str == "-" || str == "*" || str == "/" {
n := len(answer)
x , y := answer[n-2] , answer[n-1]
answer = answer[0:n-2]
answer = append(answer , getResult(x , y , str))
} else {
if value , ok := strconv.Atoi(str) ; ok == nil {
answer = append(answer , value)
}
}
}
return answer[0]
}Editor is loading...
Leave a Comment