Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Brackets by szneqz
def checkio(expression):
newstring = expression
leng = len(expression)
i = 0
j = 0
while i < leng:
sign = newstring[i]
counter = 0
if sign == '[' or sign == '{' or sign == '(':
j = i + 1
while j < leng:
sign2 = newstring[j]
if ((sign == '[' and sign2 == ']') or (sign == '{' and sign2 == '}') or (sign == '(' and sign2 == ')')) and counter <= 0:
newstring = newstring[0:i] + ' ' + newstring[i+1:j] + ' ' + newstring[j+1:leng]
#print(i, j, counter)
break
if (sign2 == '[') or (sign2 == '{') or (sign2 == '('):
counter = counter + 1
if (sign2 == ']') or (sign2 == '}') or (sign2 == ')'):
counter = counter - 1
j = j + 1
i = i + 1
i = 0
while i < leng:
sign = newstring[i]
if sign == '[' or sign == '{' or sign == '(' or sign == '}' or sign == ']' or sign == ')':
return False
i = i + 1
return True
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
checkio("[3+{1-1]}")
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"
Jan. 22, 2018