Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Brackets solved solution in Clear category for Brackets by dominieq
def only_brackets (array):
result = ""
for i in range(0, len(array),1):
if array[i]=="{" or array[i]=="(" or array[i]=="[" or array[i]=="]" or array[i]==")" or array[i]=="}":
result = result + array[i]
return result
def first_condition(brackets):
counter_1 = 0
counter_2 = 0
counter_3 = 0
for i in range(0, len(brackets)):
if brackets[i] == "(" or brackets[i] == ")":
counter_1 = counter_1 + 1
if brackets[i] == "{" or brackets[i] == "}":
counter_2 = counter_2 + 1
if brackets[i] == "[" or brackets[i] == "]":
counter_3 = counter_3 + 1
if counter_1%2 == 0 and counter_2%2 == 0 and counter_3%2 == 0:
return True
else:
return False
def second_condition(brackets):
condition = True
border = len(brackets)
for i in range(0, border):
a = ord(brackets[i])
if brackets[i] == "(" or brackets[i] == "[" or brackets[i] == "{":
condition = False
for j in range(i+1, border, 2):
if brackets[j] == ")" or brackets[j] == "]" or brackets[j] == "}":
b = ord(brackets[j])
if b - a <= 2:
condition = True
break
if condition == False:
break
return condition
def checkio(expression):
part_results = only_brackets(expression)
if first_condition(part_results) and second_condition(part_results):
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"
Oct. 29, 2016