In [2]:
from pythonds9.basic.stack import Stack
In [4]:
def par_checker(symbol_string):
    s = Stack()
    balanced = True
    index = 0
    
    while index < len(symbol_string) and balanced:
        symbol = symbol_string[index]
        
        if symbol == "(":
            s.push(symbol)
        else:  # we have a ")"
            if s.is_empty():
                # There is no left parens in the stack to match this right one
                balanced = False
            else:
                # Pop out a right parens to reduce stack size
                s.pop()
                
        index += 1
        
    return balanced and s.is_empty()
In [5]:
s = "()()()((())))"
par_checker(s)
Out[5]:
False
In [7]:
s = "(()()()((()))"
par_checker(s)
Out[7]:
False