-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1106.py
More file actions
34 lines (30 loc) · 937 Bytes
/
1106.py
File metadata and controls
34 lines (30 loc) · 937 Bytes
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
def parseBoolExpr(expression: str) -> bool:
op_stack = []
args_stack = []
for c in expression:
if c in "!&|":
op_stack.append(c)
elif c == "t":
args_stack.append(True)
elif c == "f":
args_stack.append(False)
elif c == "(":
args_stack.append(c)
elif c == ")":
res = args_stack.pop()
op = op_stack.pop()
if op == "!":
res = not res
else:
while args_stack[-1] != "(":
if op == "|":
res |= args_stack.pop()
else:
res &= args_stack.pop()
args_stack.pop()
args_stack.append(res)
return args_stack.pop()
if __name__ == '__main__':
print(parseBoolExpr("&(|(f))"))
print(parseBoolExpr("|(f,f,f,t)"))
print(parseBoolExpr("!(&(f,t))"))