Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Brackets by Bartlomiej_Szal
def bracketType(bracket):
if bracket == '{' or bracket =='}':
return 1
elif bracket == '(' or bracket == ')':
return 2
elif bracket == '[' or bracket == ']':
return 3
def checkio(expression):
openingBrackets = ["{","(","["]
closingBrackets = ["}",")","]"]
openingCount = 0
closingCount = 0
stack = []
for i in range(len(expression)):
if expression[i] in openingBrackets:
stack.append(bracketType(expression[i]))
elif expression[i] in closingBrackets:
if len(stack) > 0:
lastBraceType = stack.pop()
else:
return False
if bracketType(expression[i]) != lastBraceType:
return False
if len(stack) > 0:
return False
else:
return True
Oct. 9, 2016