Python program to check matching of simple parentheses

后端 未结 25 2339
谎友^
谎友^ 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")
    
    0 讨论(0)
  • 2020-12-01 17:36

    The problem with your approach is that you don't consider the order. Following line would pass: ))) (((. I'd suggest to keep the count of open and closed parenthesis:

    • counter starts from 0
    • every ( symbol increment counter
    • every ) symbol decrements counter
    • if at any moment counter is negative it is an error
    • if at the end of the line counter is 0 - string has matching parenthesis
    0 讨论(0)
  • 2020-12-01 17:36

    More advanced example in which you additionally need to check a matching of square brackets '[]' and braces '{}' pars.

    string = '([]{})'
    
    def group_match(string):
    
        d = {
        ')':'(',
        ']':'[',
        '}':'{'
        }
    
        list_ = []
    
        for index, item in enumerate(string):
            if item in d.values():
                list_.append(item)
    
            elif (item in d.keys()) and (d.get(item) in list_):
                list_.pop()
    
        return len(list_) == 0
    
    0 讨论(0)
  • 2020-12-01 17:37

    Most blatant error done by you is:

        if l == ")":
            clo = clo  + [")"]
        else:
            return(ope, clo)  # here
    

    By using return, you exit from function when first char not equal to "(" or ")" is encountered. Also some indentation is off.

    Minimal change which allows your code to run (although it won't give correct answers for all possible input strings) is:

    def matched(str):
        ope = []
        clo = []
        for i in range(0,len(str)):
            l = str[i]
            if l == "(":
                ope = ope + ["("]
            elif l == ")":
                clo = clo  + [")"]
        if len(ope)==len(clo):
            return True
        else:
            return False
    
    0 讨论(0)
  • 2020-12-01 17:38

    In case u also need to find the position of the first mismatching bracket from left u can use the below code which also cover certain edge cases:

    def isBalanced(expr):
        opening=set('([{')
        new=set(')]}{[(')
        match=set([ ('(',')'), ('[',']'), ('{','}') ])
        stack=[]
        stackcount=[]
        for i,char in enumerate(expr,1):
            if char not in new:
                continue
            elif char in opening:
                stack.append(char)
                stackcount.append(i)
            else:
                if len(stack)==0:
                    print(i)
                    return False
                lastOpen=stack.pop()
                lastindex=stackcount.pop()
                if (lastOpen, char) not in match:
                    print (i)
                    return False
        length=len(stack)
        if length!=0:
          elem=stackcount[0]
          print (elem)
        return length==0
    string =input()
    ans=isBalanced(string)
    if ans==True:
        print("Success")
    
    0 讨论(0)
  • 2020-12-01 17:38

    An alternative to check for balanced nested parentheses:

    def is_balanced(query: str) -> bool:
        # Alternative: re.sub(r"[^()]", "", query)
        query = "".join(i for i in query if i in {"(", ")"})
        while "()" in query:
            query = query.replace("()", "")
        return not query
    
    
    for stmt in [
        "(()()()())",    # True
        "(((())))",      # True
        "(()((())()))",  # True
        "((((((())",     # False
        "()))",          # False
        "(()()))(()",    # False
        "foo",           # True
        "a or (b and (c or d)",  # False
        "a or (b and (c or d))"  # True
        "a or (b and (c or (d and e)))",  # True
    ]:
        print(stmt)
        print("Balanced:", is_balanced(stmt))
        print()
    

    It works by:

    1. Removing everything but parentheses
    2. Recursively remove innermost parentheses pairs
    3. If you're left with anything besides the empty string, the statement is not balanced. Otherwise, it is.
    0 讨论(0)
提交回复
热议问题