Return to Snippet

Revision: 60308
at November 1, 2012 01:52 by AlexanderRavikovich


Initial Code
# 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")

Initial URL


Initial Description
Soma example of validation parentheses in math expressions. 

[{()}] - valid
[{){}] - wrong

Initial Title
Math expression syntax validator of parentheses in Python 3

Initial Tags
math

Initial Language
Python