Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Brackets by mjazy
def checkio(expression):
# elementsNumber = expression.count('{'} + expression.count('}'} + expression.count('['} + expression.count(']'}+expression.count('('} + expression.count(')'}
newWord = ''
newWordIndex = 0
rightSideIndex = 0
expressionIndex = 0
highestIndex = 0
result = True
cumulativeVariable = 1
indexesUsed = []
while expressionIndex <= len(expression)-1:
if expression[expressionIndex] == '{' or expression[expressionIndex] == '[' or expression[expressionIndex] == '(' or expression[expressionIndex] == '}' or expression[expressionIndex] == ']' or expression[expressionIndex] == ')':
newWord += expression[expressionIndex]
newWordIndex = len(newWord)-1
expressionIndex += 1
if len(newWord) < 1:
result = True
elif newWord.count('{') != newWord.count('}') or newWord.count ('[') != newWord.count(']') or newWord.count('(') != newWord.count(')'):
result = False
else:
while newWordIndex >= 0:
if newWord[newWordIndex] == '{' or newWord[newWordIndex] == '[' or newWord[newWordIndex] == '(' and newWordIndex < len(newWord)-1:
rightSideIndex = newWordIndex + 1
if newWord[rightSideIndex] == '{' or newWord[rightSideIndex] == '[' or newWord[rightSideIndex] == '(' and rightSideIndex < len(newWord)-1:
rightSideIndex += 1
else:
if newWord[newWordIndex] == '{' and newWord[rightSideIndex] == '}' and rightSideIndex not in indexesUsed:
newWordIndex -= 1
rightSideIndex = len(newWord)
indexesUsed.extend([rightSideIndex])
elif newWord[newWordIndex] == '[' and newWord[rightSideIndex] == ']' and rightSideIndex not in indexesUsed:
newWordIndex -= 1
rightSideIndex = len(newWord)
indexesUsed.extend([rightSideIndex])
elif newWord[newWordIndex] == '(' and newWord[rightSideIndex] == ')' and rightSideIndex not in indexesUsed:
newWordIndex -= 1
rightSideIndex = len(newWord)
indexesUsed.extend([rightSideIndex])
elif rightSideIndex in indexesUsed:
rightSideIndex += 1
else:
result = False
newWordIndex = -1
rightSideIndex = len(newWord)
newWordIndex -= 1
# print('Been there', highestIndex, cumulativeVariable)
# print('hello:', newString[highestIndex], newString[highestIndex+cumulativeVariable])
return result
#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"
Jan. 13, 2018