-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
38 lines (29 loc) · 910 Bytes
/
main.py
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
class Solution:
def parseBoolExpr(self, expression: str) -> bool:
i = 0
def parse() -> bool:
nonlocal i
if expression[i] == "t":
i += 1
return True
if expression[i] == "f":
i += 1
return False
if expression[i] == "!":
i += 2
result = parse()
i += 1
return not result
op = expression[i]
result = True if op == "&" else False
i += 2
while expression[i] != ")":
if expression[i] == ",":
i += 1
elif op == "&":
result = parse() and result
else: # op == "|"
result = parse() or result
i += 1
return result
return parse()