python - My code failed to iterate on somesection of the code -


i have python function code suppose balance number of "bracket" in parameter supply. though, work require problem in exercise when parameter contain 4 or more bracket function failed balance up. below code.

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  leftmost bracket or add 1 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 last rightmost bracket or add 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)")  output "false ((5+3)*2)" 

the above function work on supply parameter, when run on below supply parameter, output wrong, need advise on do.

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  leftmost bracket or add 1 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 last rightmost bracket or add 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)")  output "false ((((5+3)*2))" , wrong 

in cycle have line

final_string = str(string_input) + '\u0029' 

which means each time take input string , add ) it. no matter how many iterations final_string equal input_string joined ).


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -