Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查Python3中的不平衡括号?_Python_Python 3.x - Fatal编程技术网

如何检查Python3中的不平衡括号?

如何检查Python3中的不平衡括号?,python,python-3.x,Python,Python 3.x,如何检查不平衡支架? 我正在考虑括号-->{,[,(,),],。 如何检查字符串是否不平衡 示例: {[()]}-->平衡 {[(])}-->不平衡 {{[[(())]]}--> 我试过这种方法,但不起作用: string = '{[(])}' par = 0 col = 0 cha = 0 for i in string: if i == '{': cha+=1 if i == '}': cha-=1 if i == '[':

如何检查不平衡支架? 我正在考虑括号-->
{
[
]
。 如何检查字符串是否不平衡

示例:

  • {[()]}
    -->平衡
  • {[(])}
    -->不平衡
  • {{[[(())]]}
    -->
  • 我试过这种方法,但不起作用:

    string = '{[(])}'
    
    par = 0
    col = 0
    cha = 0
    for i in string:
        if i == '{':
            cha+=1
        if i == '}':
            cha-=1
        if i == '[':
            col+=1
        if i == ']':
            col-=1
        if i == '(':
            par+=1
        if i == ')':
            par-=1
        if (cha < 0) or (col < 0) or (par < 0):
            print('Unbalanced')
            break
    if (cha != 0) or (col != 0) or (par != 0):
        print('Unbalanced')
    else:
        print('Balanced')
    
    string='{[(])}'
    PAR=0
    col=0
    cha=0
    对于字符串中的i:
    如果i=='{':
    cha+=1
    如果i=='}':
    cha-=1
    如果i=='[':
    col+=1
    如果i==']':
    col-=1
    如果i=='(':
    par+=1
    如果i==')':
    par-=1
    如果(cha<0)或(col<0)或(par<0):
    打印(“不平衡”)
    打破
    如果(cha!=0)或(col!=0)或(par!=0):
    打印(“不平衡”)
    其他:
    打印(‘平衡’)
    
    我认为,仅仅通过记录到目前为止看到的每个字符的数量,无法验证字符串的平衡性。你看到它们的顺序也很重要,所以你必须以这样或那样的方式将它们结合起来。这里有一种方法——保留一堆“不匹配”的括号。在找到新的左括号时添加,在找到与最近的左括号匹配的右括号时弹出,如果期望匹配但没有得到匹配,则返回False。如果到达列表的末尾且堆栈为空,则字符串处于平衡状态

    def balanced(s):
        pairs = {"{": "}", "(": ")", "[": "]"}
        stack = []
        for c in s:
            if c in "{[(":
                stack.append(c)
            elif stack and c == pairs[stack[-1]]:
                stack.pop()
            else:
                return False
        return len(stack) == 0
    
    
    test_cases = ("{[()]}", "{[(])}", "{{[[(())]]}}")
    for s in test_cases:
        print(s, balanced(s))
    
    结果:

    {[()]} True
    {[(])} False
    {{[[(())]]}} True
    

    您必须遍历字符串并存储传入的字符。每次最后两个字符是
    {}
    []
    ()
    ,只需删除这些字符,并将新字符与最后存储的字符匹配(而不是删除)。继续,直到到达字符串的末尾:如果删除了所有字符,则字符串处于平衡状态

    我发现用折页书写更容易:

    >>> from functools import reduce
    >>> def f(s): return reduce(lambda acc, x: acc[:-1] if acc and acc[-1]+x in ('{}', '[]', '()') else acc+x, s)
    
    测试用例:

    >>> f('{[()]}')
    '' # balanced
    >>> f('{[(])}')
    '{[(])}' # unbalanced
    
    对于命令式版本,请参见@Kevin的答案(我的if的两个分支相当于堆栈上的pop/push)

    编辑如果要查找伪回文(
    {[]}
    是平衡的,但不是
    {}[]
    ),则可以将每个字符与从末尾开始的相同位置的字符进行匹配

    首先,检查字符串的字符数是否为偶数:

    >>> s = '{[()]}'
    >>> len(s) % 2
    0
    
    如果是这种情况,请匹配字符:

    >>> t, u = s[:len(s)//2], s[len(s)//2:]
    >>> t, u
    ('{[(', ')]}')
    >>> t, "".join(reversed(u))
    ('{[(', '}])')
    >>> [o+c for o,c in zip(t, reversed(u))]
    ['{}', '[]', '()']
    
    如果只有匹配的对,则字符串是平衡的:

    >>> [o+c for o,c in zip(t, reversed(u)) if o+c not in ('{}', '[]', '()')]
    [] # balanced
    

    如果第一个和最后一个字符是匹配的一对,请将其剥离。重复一遍

    如果您遇到第一个字符和最后一个字符不匹配的情况,则字符串是不平衡的

    def balanced(s):
        pairs = {"{": "}", "(": ")", "[": "]"}
        stack = []
        for c in s:
            if c in "{[(":
                stack.append(c)
            elif stack and c == pairs[stack[-1]]:
                stack.pop()
            else:
                return False
        return len(stack) == 0
    
    
    test_cases = ("{[()]}", "{[(])}", "{{[[(())]]}}")
    for s in test_cases:
        print(s, balanced(s))
    
    编辑:正如@blhsing所指出的,这是行不通的


    新方法:查找
    {}
    []
    ()
    ,然后将其删除。重复此操作,直到找不到更多。如果字符串为空,则表示该字符串已平衡,否则不平衡。

    创建堆栈。每次遇到以下情况时:

    • 左括号
    • 左大括号
    • 左括号
    • a留下了什么
    。。。将左边的“分隔符”推到堆栈上。当遇到右分隔符时,从堆栈中弹出最顶部的元素。弹出的元素将是最近看到的左分隔符。确保左分隔符和右分隔符匹配。e、 它们都是括号,或者都是大括号

    如果堆栈上当前的左分隔符是大括号,而当前的右分隔符是圆括号,则返回消息,说明情况不平衡

    如果您试图从已空的堆栈中弹出某些内容,请返回“不平衡”

    如果读入整个字符串,但在到达末尾时堆栈不是空的,则返回“不平衡”

    以上内容涵盖了字符串不平衡的所有情况

    def balanced(s):
        pairs = {"{": "}", "(": ")", "[": "]"}
        stack = []
        for c in s:
            if c in "{[(":
                stack.append(c)
            elif stack and c == pairs[stack[-1]]:
                stack.pop()
            else:
                return False
        return len(stack) == 0
    
    
    test_cases = ("{[()]}", "{[(])}", "{{[[(())]]}}")
    for s in test_cases:
        print(s, balanced(s))
    
    如果您读入整个字符串并

    • 完成后,堆栈为空
    • 你从来没有尝试过弹出一个空堆栈
    • 堆栈上的左分隔符始终与遇到的右分隔符匹配(例如,两者都是括号)

    然后绳子就平衡了。返回“balanced”

    以下是检查不平衡括号的替代代码:

    def is_matched(expression):
        """
        Finds out how balanced an expression is.
        With a string containing only brackets.
    
        >>> is_matched('[]()()(((([])))')
        False
        >>> is_matched('[](){{{[]}}}')
        True
        """
        opening = tuple('({[')
        closing = tuple(')}]')
        mapping = dict(zip(opening, closing))
        queue = []
    
        for letter in expression:
            if letter in opening:
                queue.append(mapping[letter])
            elif letter in closing:
                if not queue or letter != queue.pop():
                    return False
        return not queue
    
    if __name__ == '__main__':
        import doctest
        doctest.testmod()
    
    

    下面是一种基于堆栈的方法,它将组字符转换为正数/负数,使其余逻辑独立于字符处理(并允许字符串中的其他字符):


    我只会去掉字符串中的平衡块,直到我什么都没有留下,或者剩下一个无法进一步缩小的不平衡字符串:

    def is_matched(expr):
        expr = re.sub("[^][}{)(]+", "", expr)
        while expr:
            expr1 = re.sub(r"\(\)|\[\]|\{\}", "", expr)
            if expr1 == expr:
                return not expr1
            expr = expr1
        return True
    
    >>> is_matched("{[()]}")
    True
    >>> is_matched("{[(])}")
    False
    >>> is_matched("{{[[(())]]}}")
    True
    

    第一个
    re.sub()。然后循环删除所有相邻的开/闭对,并重复,直到字符串用完或相邻对用完。

    来到@kevin的答案。下面的代码通过了他的所有测试用例,但更简单

    def balanced(s):
        if s.count('[') == s.count(']') and s.count('(') == s.count(')') and s.count('{') == s.count('}'):
            return True
        else:   
            return False
    
    问这样一个问题的目的不仅仅是为了看计数,而是为了看括号作为一个整体是否有意义。如果在面试中被问到,你想让他们知道你理解背后的动机

    Prob_test_cases = ('}{', '][', ')(')
    
    大多数解决方案都会通过上述平衡或括号匹配,但理想情况下,它们应该失败。 您不会像在
    Prob\u test\u案例中那样对代码或字符串进行编码。这是我的。
    很长但可以理解,没有任何堆栈、队列或数据结构实现

    def para_check(checkme):
        open = ['(', '[', '{']
        close = [')', ']', '}']
        # assume that the result is true
        result = True
    
        # if the input is not a list then convert it to list
        if type(checkme) is not list:
            checkme = list(checkme)
    
        # if it doesnt contain at least 2 elements then return false
        if len(checkme) < 2:
            result = False
    
        # if number of closing and opening paranthesis is not the same then it is not balanced
        count_check1 = checkme.count('[') == checkme.count(']')
        count_check2 = checkme.count('(') == checkme.count(')')
        count_check3 = checkme.count('{') == checkme.count('}')
    
        # if not all of the above are true then it is unbalanced and thus...
        if not all([count_check1, count_check2, count_check3]):
            result = False
    
        def recurser(checkme, first, last):
            '''
            here start can be '[,(,{' and end can be '],),}' respectively,
            Check for a given type of bracket (any 1 of 3) see if the 
            index of the first closing bracket is greater than the first 
            opening bracket and if yes then remove them since they are a pair.
            Repeat this forever for all 3 types of brackets.
            '''            
            if first in checkme and last in checkme:
                open_index = checkme.index(first)
                closed_index = checkme.index(last)
                
                if closed_index > open_index:
                    checkme.pop(closed_index)
                    checkme.pop(open_index)
                    # recursion
                    recurser(checkme, first, last)
                else:
                    result = False
    
        recurser(checkme, '[', ']')
        recurser(checkme, '(', ')')
        recurser(checkme, '{', '}')
    
        if len(checkme) > 0:
            result = False
    
        return result
    
    def para_check(检查我):
    open=['(','[','{']
    close=[')'、']'、'}']
    #假设结果是真的
    结果=真
    #如果输入不是列表,则将其转换为列表
    如果类型(checkme)不是列表:
    checkme=列表(checkme)
    #如果它不包含至少2个元素,则返回false
    如果len(checkme)<2:
    结果=错误
    #如果关闭和打开的数量不相同,则不平衡
    count_check1=checkme.count('[')==checkme.count(']'))
    count_check2=checkme.count(“(”)==checkme.count(“)”)
    count_check3=checkme.count('{')==checkme.cou