python3中括号的数学表达式语法验证器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3中括号的数学表达式语法验证器相关的知识,希望对你有一定的参考价值。
Soma example of validation parentheses in math expressions.[{()}] - valid
[{){}] - wrong
# Mathematical expression to validate code = "[(((a+b)*c+d-e)/(f+g)-(r+j)*(k+e))]"; parentheses_open = ['(', '{', '['] parentheses_close = [')', '}', ']'] def getParenthesesType(c): if c in parentheses_open: return parentheses_open.index(c) elif c in parentheses_close: return parentheses_close.index(c) else: return 0 def validateSyntax(x): size = len(x) s = [] for i in range(0, size): if x[i] in parentheses_open: s.append(x[i]) elif x[i] in parentheses_close: if len(s)==0: return 0 if getParenthesesType(s.pop()) != getParenthesesType(x[i]): return 0 if len(s)==0: return 1 else: return 0 if validateSyntax(code): print("Valid") else: print("Wrong")
以上是关于python3中括号的数学表达式语法验证器的主要内容,如果未能解决你的问题,请参考以下文章