我有这个python函数代码,假设在任何参数供应中平衡“括号”的数量。 虽然,它适用于练习中的需要问题但是当参数包含4个或更多括号时,该功能无法平衡它。下面是代码。
def closedBracket(string_input):
import unicodedata, re
left_bracket = re.compile('[\u0028]')
right_bracket = re.compile('[\u0029]')
left_bracket = len(left_bracket.findall(string_input))
right_bracket = len(right_bracket.findall(string_input))
if left_bracket > right_bracket:
# Remove the leftmost bracket or add one last right bracket
count = 0
largest_bracket = (left_bracket - right_bracket)
while count <= largest_bracket:
final_string = str(string_input) + '\u0029'
count += 1
print('False', final_string)
elif left_bracket < right_bracket:
# Remove the last Rightmost Bracket or add another leftmost bracket
count = 0
largest_bracket = (right_bracket - left_bracket)
while count <= largest_bracket:
final_string = '\u0028' + str(string_input)
count += 1
print('False', final_string)
else:
print('True')
closedBracket("(5+3)*2)")
The output is "False ((5+3)*2)"
上面的功能对供应参数起作用,但是当我在下面的供应参数上运行时,输出是错误的,所以我需要建议做什么。
def closedBracket(string_input):
import unicodedata, re
left_bracket = re.compile('[\u0028]')
right_bracket = re.compile('[\u0029]')
left_bracket = len(left_bracket.findall(string_input))
right_bracket = len(right_bracket.findall(string_input))
if left_bracket > right_bracket:
# Remove the leftmost bracket or add one last right bracket
count = 0
largest_bracket = (left_bracket - right_bracket)
while count <= largest_bracket:
final_string = str(string_input) + '\u0029'
count += 1
print('False', final_string)
elif left_bracket < right_bracket:
# Remove the last Rightmost Bracket or add another leftmost bracket
count = 0
largest_bracket = (right_bracket - left_bracket)
while count <= largest_bracket:
final_string = '\u0028' + str(string_input)
count += 1
print('False', final_string)
else:
print('True')
closedBracket("((((5+3)*2)")
The output is "False ((((5+3)*2))" and this is wrong
答案 0 :(得分:0)
在循环中你有这一行
final_string = str(string_input) + '\u0029'
这意味着每次你输入输入字符串并添加)
。因此,无论您执行多少次迭代,final_string
始终等于input_string
加入)
。
答案 1 :(得分:0)
感谢所有来救我的人,特别是@Alik, 上述python代码的解决方案在“while循环”部分下解决。 1.此函数名称为closedBracket(),它采用数学表达式,迭代表达式,确定开括号和右括号的数量,如果括号内有不平衡数,则函数将 返回数学表达式的平衡版本 2.该功能采用参数
返工完整代码如下:
def closedBracket(string_input):
import unicodedata, re
left_bracket = re.compile('[\u0028]') # compiling unicode of left
right_bracket = re.compile('[\u0029]') # compiling unicode of right
string_holder = [] # Declaring empty List to hold split string
string_holder[:0] = string_input # Change the input string to list
left_bracket = string_holder.count('\u0028')
right_bracket = string_holder.count('\u0029')
final_string = [] # Accumulator of
if left_bracket > right_bracket:
# Remove some leftmost brackets or add more right brackets
count = 0
while count < (left_bracket - right_bracket):
final_string.append('\u0029',)
count += 1
final_string = ''.join(final_string)
print('False', string_input + final_string)
elif left_bracket < right_bracket:
#Remove the last Rightmost Brackets or add another leftmost )
count = 0
number_right_bracket = (right_bracket - left_bracket)
while count < number_right_bracket:
final_string.append('\u0028',)
count += 1
final_string = ''.join(final_string)
print('False', final_string + string_input)
else:
return True
closedBracket("5+((3)*2)))))")