educative.io

The solution has problem when encounter bracket_string = '[]]][['

def check_balance(brackets):
check = 0
for bracket in brackets:
if bracket == ‘[’:
check += 1

    elif bracket == ']':
        check -= 1

    if check < 0:
        break

return check == 0

print(check_balance(bracket_string))

I found this code has a problem when string is bracket_string = ‘[]]][[’. it should be balance but because of this line of code:
if check < 0:
break


Course: https://www.educative.io/courses/python-fundamentals-for-programmers
Lesson: https://www.educative.io/courses/python-fundamentals-for-programmers/qVEJXGPPzvy

def check_balance(brackets):
check = 0
for bracket in brackets:
if bracket == ‘[’:
check += 1

    elif bracket == ']':
        check -= 1

return check == 0

print(check_balance(bracket_string))

I think this is enough


Course: https://www.educative.io/courses/python-fundamentals-for-programmers
Lesson: https://www.educative.io/courses/python-fundamentals-for-programmers/qVEJXGPPzvy

Hello @Sean_Chrollo,

Thank you for your keen observation regarding the code you shared. You’re absolutely right; the code encounters an issue with bracket arrangements like bracket_string = '[]]][[' but in this lesson, the problem statement says that the order of brackets matters in the input string. It says “The brackets are said to be balanced if, for every opening bracket, there is a closing bracket after it.”. Also, the provided examples show that “][” is an unbalanced bracket because the closing bracket is written before the opening bracket.

I hope this clears the point. If you have any more insights or questions, feel free to reach out.

1 Like