Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit c02b901

Browse files
committed
1106 done
1 parent b6daaab commit c02b901

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

Algorithms/1106.parsing-a-boolean-expression/parsing-a-boolean-expression.go

+15-19
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@ package problem1106
33
import "strings"
44

55
func parseBoolExpr(exp string) bool {
6-
if exp == "t" {
7-
return true
8-
}
9-
if exp == "f" {
10-
return false
6+
if exp == "t" || exp == "f" {
7+
return exp == "t"
118
}
129
n := len(exp)
13-
symbol, exp := exp[0], exp[2:n-1]
14-
subs := split(exp)
15-
switch symbol {
10+
op, exp := exp[0], exp[2:n-1]
11+
switch op {
1612
case '&':
17-
return and(subs)
13+
return and(split(exp))
1814
case '|':
19-
return or(subs)
20-
default:
15+
return or(split(exp))
16+
default: // !
2117
return not(exp)
2218
}
2319
}
@@ -44,22 +40,22 @@ func split(exp string) []string {
4440
return strings.Split(exp, "@")
4541
}
4642

47-
func or(exps []string) bool {
43+
func and(exps []string) bool {
4844
for _, e := range exps {
49-
if parseBoolExpr(e) {
50-
return true
45+
if !parseBoolExpr(e) {
46+
return false
5147
}
5248
}
53-
return false
49+
return true
5450
}
5551

56-
func and(exps []string) bool {
52+
func or(exps []string) bool {
5753
for _, e := range exps {
58-
if !parseBoolExpr(e) {
59-
return false
54+
if parseBoolExpr(e) {
55+
return true
6056
}
6157
}
62-
return true
58+
return false
6359
}
6460

6561
func not(exp string) bool {

0 commit comments

Comments
 (0)