推 JIWP: 別倦了 10/20 22:24
直接看別人寫的
這種題目我真的爛到懶得響
希望我這樣抄答案能記起來==
def parseBoolExpr(self, expression: str) -> bool:
stk = []
for c in expression:
if c in ["(", ","]:
continue
elif c in ["t","f", "|", "&", "!"]:
stk.append(c)
elif c == ")":
isTrue = False
isFalse = False
while stk[-1] in ["t", "f"]:
c = stk.pop()
if c == "t":
isTrue = True
else:
isFalse = True
operator = stk.pop()
if operator == "|":
stk.append("t" if isTrue else "f")
elif operator == "&":
stk.append("f" if isFalse else "t")
elif operator == "!":
stk.append("t" if isFalse else "f")
return True if stk[0]=="t" else False
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1729434052.A.73E.html