...

/

Solution Review: Check Whether the Brackets are Balanced

Solution Review: Check Whether the Brackets are Balanced

This review explains the solution for the "balanced brackets" exercise.

We'll cover the following...

Solution

Press + to interact
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
bracket_string = '[[[[]]'
print(check_balance(bracket_string))

Explanation

The solution relies on the value of the check variable declared at line 2, which is updated in each iteration.

At ...