Parentheses pairing ({}[]()<>) issue

前端 未结 9 1554
醉酒成梦
醉酒成梦 2021-02-06 00:27

I want to be able to pair up all parentheses in a string, if they aren\'t paired then then they get their index number and False. It seems like it is repeating some values over

9条回答
  •  再見小時候
    2021-02-06 01:05

    You can adapt my code to a similar question:

    def Evaluate(str):
      stack = []
      pushChars, popChars = "<({[", ">)}]"
      for c in str :
        if c in pushChars :
          stack.append(c)
        elif c in popChars :
          if not len(stack) :
            return False
          else :
            stackTop = stack.pop()
            balancingBracket = pushChars[popChars.index(c)]
            if stackTop != balancingBracket :
              return False
        else :
          return False
      return not len(stack)
    

提交回复
热议问题