Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Brackets by jszymk
def checkio(expression):
str = ""
for i in expression :
if i == "(" or i == ")" or i == "[" or i == "]" or i == "{" or i == "}" :
str += i
str = list(str)
rm = 1
while len(str) > 0 and rm > 0:
a = 0
rm = 0
while a < len(str)-1 and len(str) > 0:
if str[a] == "(" and str[a+1] == ")" :
str.pop(a)
str.pop(a)
rm += 1
break
elif str[a] == "{" and str[a+1] == "}" :
str.pop(a)
str.pop(a)
rm += 1
break
elif str[a] == "[" and str[a+1] == "]" :
str.pop(a)
str.pop(a)
rm += 1
break
a += 1
if rm > 0 :
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("([{}{}{}{}])") == True, "No brackets, no problem"
Oct. 17, 2016