Refactor Stack

This commit is contained in:
Chris McLennon
2017-10-05 22:24:02 -05:00
parent aa8485b4df
commit 17e1a92f49
6 changed files with 151 additions and 125 deletions

View File

@@ -0,0 +1,21 @@
from Stack import Stack
__author__ = 'Omkar Pathak'
def balanced_parentheses(parentheses):
""" Use a stack to check if a string of parentheses are balanced."""
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)))