-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (45 loc) · 1.06 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
fmt.Println("Enter your score sperated by spaces and in capital letters:")
reader := bufio.NewReader(os.Stdin)
rawInput, _ := reader.ReadString('\r')
rawInput = strings.Replace(rawInput, "\r", " ", -1)
ops := strings.Split(rawInput, " ")
fmt.Println(calPoints(ops))
}
func calPoints(ops []string) int {
var res int = 0
var totalSlice = []int{}
for _, value := range ops {
val, er := strconv.ParseInt(value, 10, 32)
if er != nil {
var tempTotalSlice []int
switch value {
case "C":
for i := 0; i < (len(totalSlice) - 1); i++ {
tempTotalSlice = append(tempTotalSlice, totalSlice[i])
}
totalSlice = tempTotalSlice
case "D":
totalSlice = append(totalSlice, 2*totalSlice[len(totalSlice)-1])
case "+":
totalSlice = append(totalSlice, totalSlice[len(totalSlice)-1]+totalSlice[len(totalSlice)-2])
default:
}
}
if er == nil {
totalSlice = append(totalSlice, int(val))
}
}
for _, val := range totalSlice {
res += val
}
return res
}