This repository was archived by the owner on Sep 20, 2023. It is now read-only.
File tree 1 file changed +15
-19
lines changed
Algorithms/1106.parsing-a-boolean-expression
1 file changed +15
-19
lines changed Original file line number Diff line number Diff line change @@ -3,21 +3,17 @@ package problem1106
3
3
import "strings"
4
4
5
5
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"
11
8
}
12
9
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 {
16
12
case '&' :
17
- return and (subs )
13
+ return and (split ( exp ) )
18
14
case '|' :
19
- return or (subs )
20
- default :
15
+ return or (split ( exp ) )
16
+ default : // !
21
17
return not (exp )
22
18
}
23
19
}
@@ -44,22 +40,22 @@ func split(exp string) []string {
44
40
return strings .Split (exp , "@" )
45
41
}
46
42
47
- func or (exps []string ) bool {
43
+ func and (exps []string ) bool {
48
44
for _ , e := range exps {
49
- if parseBoolExpr (e ) {
50
- return true
45
+ if ! parseBoolExpr (e ) {
46
+ return false
51
47
}
52
48
}
53
- return false
49
+ return true
54
50
}
55
51
56
- func and (exps []string ) bool {
52
+ func or (exps []string ) bool {
57
53
for _ , e := range exps {
58
- if ! parseBoolExpr (e ) {
59
- return false
54
+ if parseBoolExpr (e ) {
55
+ return true
60
56
}
61
57
}
62
- return true
58
+ return false
63
59
}
64
60
65
61
func not (exp string ) bool {
You can’t perform that action at this time.
0 commit comments