字符串处理——判断括号是否平衡
String processing - Determine if parenthesis are balanced
我正在使用读取字符串输入并计算括号(使用任意字符来打开和关闭)是否平衡的代码。目的是提示用户输入括号的数量作为字符串,以便编译器计算它们的数量和类型(例如,如果它是 '('
或 ')'
)。
我得到提示:
Hint1: introduce two counters initialized to zero in the beginning.
Then explore the symbols of the string in a loop. For the current
symbol increment the left
counter by 1 if the symbol is '('
,
otherwise, increment by 1 the `right' counter
Hint2: call a string math-like if the brackets occur like in a
mathematical formula. For instance, the strings '()'
, '(())()'
,
'(()())'
are balanced, while the strings '))(())(('
and '())(()'
are not.
我的代码现在看起来像这样:
lefts =str(input("Please enter a left parenthesis: "))
rights =str(input("Please enter a right parenthesis: "))
#token parsing requires paying attention to the order of the parenthesis
def ptest(test): #testing with counters
lefts = 0
rights = 0
balance = 0 #additional counter that will help us determine the amount of strings and their relation to each other
for c in test:
if c == '(':
balance += 1
lefts += 1
elif c == ')':
balance -= 1
rights += 1
print ('testing "'+test+'"')
print ('lefts='+str(lefts)+' rights='+str(rights))
#if there will b a balance between the strings/parenthesis, they will possibly be balanced
if balance == 0: print 'same number of open/close, possibly balanced'
else: print 'different number of open/close, definitely not balanced'
ptest(test1)
ptest(test2)
我如何修改它才能使其正常工作?
但我认为您部分误解了任务。
中括号可以有很多个,但")"不能多于"("(最后两种类型的个数必须相等)
而且我认为,输入不仅包含方括号
所以你应该建立一个循环(就像你所做的那样)并且
- 测试该字符是否为括号
- 如果是括号,就增加匹配的counter(一个Counter也可以,但是两个更容易理解
- 检查收盘 Counter 是否小于开盘(如果不是,那么你可以打破循环,因为它不是数学的)
- 循环结束后,您必须检查循环中是否有错误或两个计数器不相等。在这两种情况下,字符串都不是 "mathematical",否则就是
我可以在您的代码中完成,但我认为,您应该单独完成。这些事情大部分你都已经做了,但是你似乎没有完全理解,你做了什么,必须做什么。
我正在使用读取字符串输入并计算括号(使用任意字符来打开和关闭)是否平衡的代码。目的是提示用户输入括号的数量作为字符串,以便编译器计算它们的数量和类型(例如,如果它是 '('
或 ')'
)。
我得到提示:
Hint1: introduce two counters initialized to zero in the beginning. Then explore the symbols of the string in a loop. For the current symbol increment the
left
counter by 1 if the symbol is'('
, otherwise, increment by 1 the `right' counterHint2: call a string math-like if the brackets occur like in a mathematical formula. For instance, the strings
'()'
,'(())()'
,'(()())'
are balanced, while the strings'))(())(('
and'())(()'
are not.
我的代码现在看起来像这样:
lefts =str(input("Please enter a left parenthesis: "))
rights =str(input("Please enter a right parenthesis: "))
#token parsing requires paying attention to the order of the parenthesis
def ptest(test): #testing with counters
lefts = 0
rights = 0
balance = 0 #additional counter that will help us determine the amount of strings and their relation to each other
for c in test:
if c == '(':
balance += 1
lefts += 1
elif c == ')':
balance -= 1
rights += 1
print ('testing "'+test+'"')
print ('lefts='+str(lefts)+' rights='+str(rights))
#if there will b a balance between the strings/parenthesis, they will possibly be balanced
if balance == 0: print 'same number of open/close, possibly balanced'
else: print 'different number of open/close, definitely not balanced'
ptest(test1)
ptest(test2)
我如何修改它才能使其正常工作?
但我认为您部分误解了任务。
中括号可以有很多个,但")"不能多于"("(最后两种类型的个数必须相等) 而且我认为,输入不仅包含方括号
所以你应该建立一个循环(就像你所做的那样)并且
- 测试该字符是否为括号
- 如果是括号,就增加匹配的counter(一个Counter也可以,但是两个更容易理解
- 检查收盘 Counter 是否小于开盘(如果不是,那么你可以打破循环,因为它不是数学的)
- 循环结束后,您必须检查循环中是否有错误或两个计数器不相等。在这两种情况下,字符串都不是 "mathematical",否则就是
我可以在您的代码中完成,但我认为,您应该单独完成。这些事情大部分你都已经做了,但是你似乎没有完全理解,你做了什么,必须做什么。