This is my solution
def balanced_brackets():
stack = list()
elementos = {"[":"]","{":"}","(":")"}
keys = list()
values = list()
for i,j in elementos.items():
values.append(j)
keys.append(i)
item = input("Ingrese -> ")
for i in item:
if i in keys:
stack.append(i)
if i in values:
for a,b in elementos.items():
if stack != [] and i == b:
if a == stack[-1]:
stack.pop()
else:
stack.append(i)
if stack == []:
resultado = "Balanceado"
else:
resultado = "Desbalanceado"
return resultado
0 answers ( 0 marked as helpful)