Python program to check matching of simple parentheses

后端 未结 25 2340
谎友^
谎友^ 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:19

    if "(" ,")" these two characters are not present then we don't want to return true or false just return no matching found. if matching found i just checking the count of both characters are same then return true, else return false

    def matched(str):
       count1=0
       count2=1
       for i in str:
           if i =="(":
               count1+=1:
           elif i==")":
               count2+=1:
           else:
               print "no matching found for (,)"
       if count1==count2:
            return True
       else:
            return False
    
    0 讨论(0)
  • 2020-12-01 17:19
        #function to check if number of closing brackets is equal to the number of opening brackets
        #this function also checks if the closing bracket appears after the opening bracket
        def matched(str1):
            if str1.count(")")== str1.count("("):
                p1=str1.find("(")
                p2=str1.find(")")
                if p2 >= p1:
                    str1=str1[p1+1:p2]+ str1[p2+1:]
                    if str1.count(")")>0 and str1.count("(")>0:
                        matched(str1)
                    return True
                else:
                    return False
            else:
                return False
    
        matched(str1)
    
    0 讨论(0)
  • 2020-12-01 17:22

    The simplest code ever!!

    def checkpar(x):
        while len(''.join([e for e in x if e in "()"]).split('()'))>1: x=''.join(x.split('()'))
        return not x
    
    0 讨论(0)
  • 2020-12-01 17:22
    parenthesis_String = input("Enter your parenthesis string")
    parenthesis_List = []
    for p in parenthesis_String:
        parenthesis_List.append(p)
    print(parenthesis_List)
    
    if len(parenthesis_List)%2 != 0:
        print("Not Balanced Wrong number of input")
    
    for p1 in parenthesis_List:
        last_parenthesis = parenthesis_List.pop()
        print(last_parenthesis)
        if (p1 == '{' and last_parenthesis == '}' or p1 == '[' and last_parenthesis == ']' or p1 == '(' and last_parenthesis == ')'):
            print("Balanced")
        else:
            print("Not balanced")
    
    0 讨论(0)
  • 2020-12-01 17:23
    a = "((a+b)*c)+(b*a))"
    
    li = list(a)
    result = []
    
    for i in range(0, len(a)):
    
        if a[i] == "(":
            result.append(i)
        elif a[i] == ")":
            if len(result) > 0:
                result.pop()
            else:
                li.pop(i)
    
    for i in range(0, len(result)):
        li.pop(result[i])
    
    print("".join(li))
    
    0 讨论(0)
  • 2020-12-01 17:23

    just modified Henry Prickett-Morgan's code a little bit to handle it more sensibly, namely taking into account that the number of "(" matches that of ")" but string starts with ")" or ends with "(" which are apparently not right.

    
    def ValidParenthesis(s):
        count = 0
        if s[0] == ')' or s[-1] == '(':
          return False
        else:
          for c in s:
            if c == '(':
              count += 1
            elif c == ')':
              count -= 1
            else:
              continue
          return count == 0
    
    
    0 讨论(0)
提交回复
热议问题