Python program to check matching of simple parentheses

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

    def parenthesis_check(parenthesis):
        chars = []
        matches = {')':'(',']':'[','}':'{'}
        for i in parenthesis:
            if i in matches:
                if chars.pop() != matches[i]:
                    return False
            else:
                chars.append(i)
        return chars == []        
    

提交回复
热议问题