Answers for "Python Valid Parentheses"

0

Python Valid Parentheses

OPENING = '('


def is_balanced(parentheses):
    stack = []
    for paren in parentheses:
        if paren == OPENING:
            stack.append(paren)
        else:
            try:
                stack.pop()
            except IndexError:  # too many closing parens
                return False
    return len(stack) == 0  # false if too many opening parens


is_balanced('((()))')  # => True
is_balanced('(()')  # => False
is_balanced('())')  # => False
Posted by: Guest on October-24-2021

Code answers related to "Python Valid Parentheses"

Python Answers by Framework

Browse Popular Code Answers by Language