Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func evalRPN(tokens []string) int {
- stack:=[]int{}
- operators:= map[string]func(int,int) int {
- "+": func (a,b int) int { return a + b},
- "-": func(a, b int) int { return a - b },
- "*": func(a, b int) int { return a * b },
- "/": func(a, b int) int { return a / b },
- }
- for _,token:= range tokens {
- if calc,f:= operators[token]; f {
- a,b:=stack[len(stack)-2],stack[len(stack)-1]
- val:= calc(a,b)
- stack = append(stack[:len(stack)-2], val)
- }else {
- num,_:= strconv.Atoi(token)
- stack=append(stack,num)
- }
- }
- return stack[0]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement