Hi all:
I’m a newbie to Golang and I’m learning Golang from Miek Gieben’s “Learning Go” , when I try his answer code of “Number cruncher”,it gave the wrong answer.
here is my simplified code just for debug.
package main
import (
"fmt"
"strconv"
)
const (
_ = 1000 * iota
ADD
SUB
MUL
DIV
MAXPOS = 11
)
var mop = map[int]string{ADD: "+", SUB: "-", MUL: "*", DIV: "/"}
func rpnstr(r []int) (ret string) {
s := make([]string, 0)
for k, t := range r {
switch t {
case ADD, SUB, MUL, DIV:
fmt.Printf("k=%d s0=%v len(s)=%d \n", k, s, len(s))
a, s := s[len(s)-1], s[:len(s)-1]
b, s := s[len(s)-1], s[:len(s)-1]
if k == len(r)-1 {
s = append(s, b+mop[t]+a)
} else {
s = append(s, "("+b+mop[t]+a+")")
}
fmt.Printf("k=%d s1=%v len(s)=%d \n", k, s, len(s))
default:
{
s = append(s, strconv.Itoa(t))
}
}
}
for _, v := range s {
ret += v
}
return
}
func main() {
list := []int{75, 8, 6, MUL, ADD, 8, MUL, 7, SUB, 1, DIV}
print(rpnstr(list))
}
it’s a part of Reverse Polish notation,we could see the output log by run it. just like:
k=3 s0=[75 8 6] len(s)=3
k=3 s1=[75 (8*6)] len(s)=2
k=4 s0=[75 (8*6) 6] len(s)=3
k=4 s1=[75 ((8*6)+6)] len(s)=2
k=6 s0=[75 ((8*6)+6) 6 8] len(s)=4
......
$ go version
go version go1.8.1 windows/amd64
when k=3 (t = MUL), it pop 6 and 8 and combinated as "(86) which is just we want,but in the next loop ,when k=4 (t = ADD), the value of slice s changed from [75 (86)] to [75 (8*6) 6] itself without any operation,where’s the last 6 come from? I think Miek’s answer is right but I couldn’t tell what’s wrong with it.
Anybody knows? Any idea will be appreciated! thanks.