2018-10-19 12:48:28 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from .Stack import Stack
|
|
|
|
|
|
|
|
__author__ = 'Omkar Pathak'
|
|
|
|
|
|
|
|
|
|
|
|
def balanced_parentheses(parentheses):
|
2018-10-30 14:29:12 +00:00
|
|
|
""" Use a stack to check if a string of parentheses is balanced."""
|
2018-10-19 12:48:28 +00:00
|
|
|
stack = Stack(len(parentheses))
|
|
|
|
for parenthesis in parentheses:
|
|
|
|
if parenthesis == '(':
|
|
|
|
stack.push(parenthesis)
|
|
|
|
elif parenthesis == ')':
|
|
|
|
stack.pop()
|
|
|
|
return not stack.is_empty()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
examples = ['((()))', '((())']
|
|
|
|
print('Balanced parentheses demonstration:\n')
|
|
|
|
for example in examples:
|
|
|
|
print(example + ': ' + str(balanced_parentheses(example)))
|