aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--150/go.mod5
-rw-r--r--150/go.sum2
-rw-r--r--150/main.go63
3 files changed, 70 insertions, 0 deletions
diff --git a/150/go.mod b/150/go.mod
new file mode 100644
index 0000000..0248500
--- /dev/null
+++ b/150/go.mod
@@ -0,0 +1,5 @@
+module 150
+
+go 1.19
+
+require github.com/emirpasic/gods v1.18.1
diff --git a/150/go.sum b/150/go.sum
new file mode 100644
index 0000000..b5ad666
--- /dev/null
+++ b/150/go.sum
@@ -0,0 +1,2 @@
+github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
+github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
diff --git a/150/main.go b/150/main.go
new file mode 100644
index 0000000..1953134
--- /dev/null
+++ b/150/main.go
@@ -0,0 +1,63 @@
+package main
+
+import (
+ "fmt"
+ "strconv"
+
+ "github.com/emirpasic/gods/stacks/arraystack"
+)
+
+func evalRPN(tokens []string) int {
+ st := arraystack.New()
+ for _, op := range tokens {
+ switch op {
+ case "+":
+ {
+ val1, _ := st.Pop()
+ val2, _ := st.Pop()
+ res := val1.(int) + val2.(int)
+ fmt.Printf("%d + %d = %d\n", val1, val2, res)
+ st.Push(res)
+ }
+ case "-":
+ {
+ val1, _ := st.Pop()
+ val2, _ := st.Pop()
+ res := val2.(int) - val1.(int)
+ fmt.Printf("%d - %d = %d\n", val2, val1, res)
+ st.Push(res)
+ }
+ case "*":
+ {
+ val1, _ := st.Pop()
+ val2, _ := st.Pop()
+ res := val1.(int) * val2.(int)
+ fmt.Printf("%d * %d = %d\n", val1, val2, res)
+ st.Push(res)
+ }
+ case "/":
+ {
+ val1, _ := st.Pop()
+ val2, _ := st.Pop()
+ res := val2.(int) / val1.(int)
+ fmt.Printf("%d / %d = %d\n", val2, val1, res)
+ st.Push(res)
+ }
+ default:
+ {
+ val1, _ := strconv.Atoi(op)
+ st.Push(val1)
+ }
+ }
+ }
+
+ final, _ := st.Pop()
+
+ return final.(int)
+}
+
+func main() {
+ fmt.Println(evalRPN([]string{"2", "1", "+", "3", "*"}))
+ fmt.Println(evalRPN([]string{"4", "13", "5", "/", "+"}))
+ fmt.Println(evalRPN([]string{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}))
+}