Python 如果可能的话,在脚本的最后一部分需要帮助吗

Python 如果可能的话,在脚本的最后一部分需要帮助吗,python,Python,这是我剧本的先决条件 "Generate a string with N opening brackets ("[") and N closing brackets ("]"), in some arbitrary order. You will need to use random numbers. Determine whether the generated string is balanced; that is, whether it consists entirely

这是我剧本的先决条件

"Generate a string with N opening brackets ("[") and N closing brackets     ("]"), in some
arbitrary order. You will need to use random numbers.
Determine whether the generated string is balanced; that is, whether it     consists entirely of
pairs of opening/closing brackets (in that order), none of which mis-nest.
Examples:
 [] OK ][ NOT OK
[][] OK ][][ NOT OK
[[][]] OK []][[] NOT OK
"

我已经想到了这一点,并认为我已经完成了,但我只是相信它并没有完全越过界限,因为我在“[]][”中得到了一个积极的结果


有人能帮我完成这部分“成对的开/关括号(按顺序)”吗

!/usr/bin/python
导入字符串
随机输入
def方括号():
计数=0
sample=[random.choice(['[',']','[]'])用于范围内的uuu(random.randint(1,10))]
sample2='''.join([random.choice(['[',']','[]']),用于范围内的uuuu(random.randint(1,10))]))
count1=sample2.count(“[”)
count2=sample2.count(']')
对于样本2中的x:
如果x==“[”:
计数+=1
如果x==“]”:
计数-=1
如果计数!=0或计数<0:
打印“生成的样本为%s%”(样本2)
打印“有%d[在生成的字符串中](count1,)
打印“生成的字符串中有%d%”(count2,)
打印“此字符串不正常”
如果计数=0:
打印“生成的样本为%s%”(样本2)
打印“有%d[在生成的字符串中](count1,)
打印“生成的字符串中有%d%”(count2,)
打印“此字符串正常”
打印括号()

最简单的方法是使用桩。下面的代码将为您提供答案:

from pythonds.basic.stack import Stack

def parChecker(symbolString):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol in "([{":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if not matches(top,symbol):
                       balanced = False
        index = index + 1
    if balanced and s.isEmpty():
        return True
    else:
        return False

def matches(open,close):
    opens = "([{"
    closers = ")]}"
    return opens.index(open) == closers.index(close)


print(parChecker('{{([][])}()}'))
print(parChecker('[{()]'))
从pythonds.basic.stack导入堆栈
def包裹检查器(符号串):
s=堆栈()
平衡=正确
索引=0
当索引

在这段代码中,只需将每个开始符号推到堆栈上,等待匹配的结束符号稍后出现在序列中。当结束符号出现时,我们必须检查它是否正确匹配堆栈顶部开始符号的类型。如果两个符号不匹配,则字符串不平衡。如果重新处理字符串,堆栈上没有任何内容,字符串已正确平衡。

最简单的方法是使用Staks。下面的代码将为您提供答案:

from pythonds.basic.stack import Stack

def parChecker(symbolString):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol in "([{":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if not matches(top,symbol):
                       balanced = False
        index = index + 1
    if balanced and s.isEmpty():
        return True
    else:
        return False

def matches(open,close):
    opens = "([{"
    closers = ")]}"
    return opens.index(open) == closers.index(close)


print(parChecker('{{([][])}()}'))
print(parChecker('[{()]'))
从pythonds.basic.stack导入堆栈
def包裹检查器(符号串):
s=堆栈()
平衡=正确
索引=0
当索引

在这段代码中,只需将每个开始符号推到堆栈上,等待匹配的结束符号稍后出现在序列中。当结束符号出现时,我们必须检查它是否正确匹配堆栈顶部开始符号的类型。如果两个符号不匹配,则字符串不平衡。如果重新处理字符串,堆栈上没有任何内容,字符串已正确平衡。

最简单的方法是使用Staks。下面的代码将为您提供答案:

from pythonds.basic.stack import Stack

def parChecker(symbolString):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol in "([{":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if not matches(top,symbol):
                       balanced = False
        index = index + 1
    if balanced and s.isEmpty():
        return True
    else:
        return False

def matches(open,close):
    opens = "([{"
    closers = ")]}"
    return opens.index(open) == closers.index(close)


print(parChecker('{{([][])}()}'))
print(parChecker('[{()]'))
从pythonds.basic.stack导入堆栈
def包裹检查器(符号串):
s=堆栈()
平衡=正确
索引=0
当索引

在这段代码中,只需将每个开始符号推到堆栈上,等待匹配的结束符号稍后出现在序列中。当结束符号出现时,我们必须检查它是否正确匹配堆栈顶部开始符号的类型。如果两个符号不匹配,则字符串不平衡。如果重新处理字符串,堆栈上没有任何内容,字符串已正确平衡。

最简单的方法是使用Staks。下面的代码将为您提供答案:

from pythonds.basic.stack import Stack

def parChecker(symbolString):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol in "([{":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if not matches(top,symbol):
                       balanced = False
        index = index + 1
    if balanced and s.isEmpty():
        return True
    else:
        return False

def matches(open,close):
    opens = "([{"
    closers = ")]}"
    return opens.index(open) == closers.index(close)


print(parChecker('{{([][])}()}'))
print(parChecker('[{()]'))
从pythonds.basic.stack导入堆栈
def包裹检查器(符号串):
s=堆栈()
平衡=正确
索引=0
当索引