Given an expression with only ‘}’ and ‘{‘. The expression may not be balanced. Find the minimum number of bracket reversals to make the expression balanced
…. python `` a=['}{}{}{}}}{{{{{}{}{}}{{}{}{}}{{}}{{']
for elem in a:
sol=0
stack=[]
#stack.append(elem[i])
i=0
while i<len(elem)-1:
if elem[i]=='{' and elem[i+1]=='{':
stack.append(elem[i])
stack.append(elem[i+1])
sol+=1
elif elem[i]=='}' and elem[i+1]=='{':
if len(stack)!=0:
if stack[-1]=='{':
stack.pop()
stack.append(elem[i+1])
else:
stack.append(elem[i])
stack.append(elem[i+1])
sol+=1
else:
stack.append(elem[i])
`` stack.append(elem[i+1])
sol+=2
elif elem[i]=='}' and elem[i+1]=='}':
if len(stack)!=0:
if stack[-1]=='{' and stack[-2]=='{':
stack.pop()
stack.pop()
sol-=1
elif stack[-1]=='{' and stack[-2]=='}':
stack.pop()
stack.append(elem[i+1])
else:
stack.append(elem[i])
stack.append(elem[i+1])
sol+=1
else:
stack.append(elem[i])
stack.append(elem[i+1])
sol+=1
i+=2
print(sol)
….
expected 5 output 6