the nested brackets problem is a problem that determines if a sequence of brackets are properly nested a sequence of brackets s is considered properly nested if any of the following conditions are true s is empty s has the form u or u or u where u is a properly nested string s has the form vw where v and w are properly nested strings for example the string is properly nested but is not the function called isbalanced takes as input a string s which is a sequence of brackets and returns true if s is nested and false otherwise isbalanced true isbalanced true isbalanced true isbalanced true isbalanced true isbalanced true isbalanced false isbalanced true isbalanced false isbalanced false isbalanced true isbalanced true isbalanced false isbalancedlife is a bowl of cherries true isbalancedlife is a bowl of cheies true isbalancedlife is a bowl of cheies false is_balanced true is_balanced true is_balanced true is_balanced true is_balanced true is_balanced true is_balanced false is_balanced true is_balanced false is_balanced false is_balanced true is_balanced true is_balanced false is_balanced life is a bowl of cherries true is_balanced life is a bowl of che ies true is_balanced life is a bowl of che ies false stack should be empty
def is_balanced(s: str) -> bool:
    
    open_to_closed = {"{": "}", "[": "]", "(": ")"}
    stack = []
    for symbol in s:
        if symbol in open_to_closed:
            stack.append(symbol)
        elif symbol in open_to_closed.values() and (
            not stack or open_to_closed[stack.pop()] != symbol
        ):
            return False
    return not stack  


def main():
    s = input("Enter sequence of brackets: ")
    print(f"'{s}' is {'' if is_balanced(s) else 'not '}balanced.")


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    main()