From 2d082cf19c91b40a0808af0acdc0970f93f536b9 Mon Sep 17 00:00:00 2001 From: Mikael Souza Date: Mon, 17 Dec 2018 10:44:38 -0400 Subject: [PATCH 1/3] Changed import from .Stack to stack --- data_structures/stacks/balanced_parentheses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index 02efa8980291..ef470781de6a 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -1,6 +1,6 @@ from __future__ import print_function from __future__ import absolute_import -from .Stack import Stack +from stack import Stack __author__ = 'Omkar Pathak' From a8cfc14737bab805a7102accf24afafe565ef3f9 Mon Sep 17 00:00:00 2001 From: Mikael Souza Date: Mon, 17 Dec 2018 10:45:16 -0400 Subject: [PATCH 2/3] Added more parentheses examples --- data_structures/stacks/balanced_parentheses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index ef470781de6a..96a4a04325fa 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -17,7 +17,7 @@ def balanced_parentheses(parentheses): if __name__ == '__main__': - examples = ['((()))', '((())'] + examples = ['((()))', '((())', '(()))'] print('Balanced parentheses demonstration:\n') for example in examples: print(example + ': ' + str(balanced_parentheses(example))) From 2e2fadf4db224e53874b01fde0cf79e15eabc3b4 Mon Sep 17 00:00:00 2001 From: Mikael Souza Date: Mon, 17 Dec 2018 10:45:54 -0400 Subject: [PATCH 3/3] Fixed bug where an empty stack would cause error --- data_structures/stacks/balanced_parentheses.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index 96a4a04325fa..3229d19c8621 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -12,8 +12,10 @@ def balanced_parentheses(parentheses): if parenthesis == '(': stack.push(parenthesis) elif parenthesis == ')': + if stack.is_empty(): + return False stack.pop() - return not stack.is_empty() + return stack.is_empty() if __name__ == '__main__':