Balance Brackets Problem

rajeevsinghthakur · September 16, 2021 · Python
open_list = ["[","{","("]
close_list = ["]","}",")"]
def check(mysrt):
    stack =[]
    for i in mysrt:
        if i in open_list:
            stack.append(i)
        elif i in close_list:
            pos = close_list.index(i)
            if ((len(stack) > 0) and
                (open_list[pos] == stack[len(stack)-1])):
                stack.pop()
                
            else:
                return "unbalance"
    if len(stack)==0:
        return "balance"
    else:
        return "unbalance"
    
s = "{)}"
print(check(s))

Comments

Please sign up or log in to contribute to the discussion.