Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Brackets by aieozn
brackets=["[","]","(",")","{","}"];
def por(a,b):
if(a=="(" and b==")"):
return True;
elif(a=="[" and b=="]"):
return True;
elif(a=="{" and b=="}"):
return True;
else:
return False;
def checkio(expression):
tab=[];
us=1
k=0
for i in range(len(expression)):
if expression[i] in brackets:
tab.append(expression[i]);
for z in range(len(tab)):
k=0;
us=0
if len(tab)==0:
break
while(k!=1):
if por(tab[us-1],tab[us]):
tab.pop(us)
tab.pop(us-1)
k=1
us=us+1;
if(us==len(tab)):
k=1
print(tab);
if tab==[]:
return True
else:
return False
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio("((5+3)*2+1)") == True, "Simple"
assert checkio("{[(3+1)+2]+}") == True, "Different types"
assert checkio("(3+{1-1)}") == False, ") is alone inside {}"
assert checkio("[1+1]+(2*2)-{3/3}") == True, "Different operators"
assert checkio("(({[(((1)-2)+3)-3]/3}-3)") == False, "One is redundant"
assert checkio("2+3") == True, "No brackets, no problem"
Nov. 27, 2016