Python program to check matching of simple parentheses

后端 未结 25 2398
谎友^
谎友^ 2020-12-01 17:02

I am a Python newbie and I came across this exercise of checking whether or not the simple brackets \"(\", \")\" in a given string are matched evenly.

I have seen ex

25条回答
  •  青春惊慌失措
    2020-12-01 17:34

    input_str = "{[()](){}}"
    strblance=""
    
    for i in input_str:
    
        if not strblance:
            strblance = strblance+i
        elif (i is '}' and strblance[len(strblance)-1] is '{') \
            or ( i is']'and strblance[len(strblance)-1] is '[') \
            or ( i is ')'and strblance[len(strblance)-1] is '('):
                strblance = strblance[:len(strblance)-1]
        else:
            strblance = strblance+i
    
    if not strblance:
    
        print ("balanced")
    
    else:
    
        print ("Not balanced")
    

提交回复
热议问题