-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.go
47 lines (43 loc) · 1.38 KB
/
solution.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
func parseBoolExpr(expression string) bool {
stack := []rune{}
for _, c := range expression {
if c == ')' {
subExpr := []rune{}
for stack[len(stack)-1] != '(' {
subExpr = append(subExpr, stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
stack = stack[:len(stack)-1] // Remove '('
op := stack[len(stack)-1]
stack = stack[:len(stack)-1] // Remove operator
if op == '!' {
if subExpr[0] == 't' {
stack = append(stack, 'f')
} else {
stack = append(stack, 't')
}
} else if op == '&' {
result := 't'
for _, e := range subExpr {
if e == 'f' {
result = 'f'
break
}
}
stack = append(stack, result)
} else if op == '|' {
result := 'f'
for _, e := range subExpr {
if e == 't' {
result = 't'
break
}
}
stack = append(stack, result)
}
} else if c != ',' {
stack = append(stack, c)
}
}
return stack[0] == 't'
}