From fb23927383eec089b02b69fe35a231ac7b576fba Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 27 Oct 2020 09:59:02 +0800 Subject: [PATCH 1/8] Fixed balanced_parentheses.py --- .../stacks/balanced_parentheses.py | 54 ++++++++++++++----- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index 7aacd5969277..b1f23254fdf9 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -1,23 +1,53 @@ from .stack import Stack -__author__ = "Omkar Pathak" - -def balanced_parentheses(parentheses): - """ Use a stack to check if a string of parentheses is balanced.""" - stack = Stack(len(parentheses)) - for parenthesis in parentheses: - if parenthesis == "(": - stack.push(parenthesis) - elif parenthesis == ")": - if stack.is_empty(): +def balanced_parentheses(parentheses: str) -> bool: + """ Use a stack to check if a string of parentheses is balanced. + >>> balanced_parentheses("([]{})") + True + >>> balanced_parentheses("[()]{}{[()()]()}") + True + >>> balanced_parentheses("[(])") + False + >>> + """ + stack = Stack() + for bracket in parentheses: + if bracket in ("(", "[", "{"): + stack.push(bracket) + elif bracket in (")", "]", "}"): + if stack.is_empty() or not is_paired(stack.pop(), bracket): return False - stack.pop() return stack.is_empty() +def is_paired(left_bracket: str, right_bracket: str) -> bool: + """ + >>> brackets = {"(" : ")", "[" : "]", "{" : "}"} + >>> for left_bracket, right_bracket in brackets.items(): + ... assert is_paired(left_bracket, right_bracket) + >>> is_paired("(", "}") + False + >>> is_paired("(", "]") + False + """ + return ( + left_bracket == "(" and right_bracket == ")" or + left_bracket == "[" and right_bracket == "]" or + left_bracket == "{" and right_bracket == "}" + ) + + if __name__ == "__main__": + from doctest import testmod + + testmod() + examples = ["((()))", "((())", "(()))"] print("Balanced parentheses demonstration:\n") for example in examples: - print(example + ": " + str(balanced_parentheses(example))) + print( + example, "is", + "balanced" if balanced_parentheses(example) + else "not balanced" + ) From 717bd10aa06509aa36ac3929a404a41076ec83ad Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 27 Oct 2020 10:08:29 +0800 Subject: [PATCH 2/8] fixed pre-commit --- data_structures/stacks/balanced_parentheses.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index b1f23254fdf9..3b3a8299218b 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -2,7 +2,7 @@ def balanced_parentheses(parentheses: str) -> bool: - """ Use a stack to check if a string of parentheses is balanced. + """Use a stack to check if a string of parentheses is balanced. >>> balanced_parentheses("([]{})") True >>> balanced_parentheses("[()]{}{[()()]()}") @@ -32,9 +32,9 @@ def is_paired(left_bracket: str, right_bracket: str) -> bool: False """ return ( - left_bracket == "(" and right_bracket == ")" or - left_bracket == "[" and right_bracket == "]" or - left_bracket == "{" and right_bracket == "}" + left_bracket == "(" and right_bracket == ")" or + left_bracket == "[" and right_bracket == "]" or + left_bracket == "{" and right_bracket == "}" ) @@ -47,7 +47,7 @@ def is_paired(left_bracket: str, right_bracket: str) -> bool: print("Balanced parentheses demonstration:\n") for example in examples: print( - example, "is", - "balanced" if balanced_parentheses(example) - else "not balanced" + example, + "is", + "balanced" if balanced_parentheses(example) else "not balanced", ) From 772471265f81cb0aeb220525efa6ab20596d98b7 Mon Sep 17 00:00:00 2001 From: shellhub Date: Wed, 28 Oct 2020 17:19:21 +0800 Subject: [PATCH 3/8] eliminate is_paired --- .../stacks/balanced_parentheses.py | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index 3b3a8299218b..c584aeb1b62b 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -12,42 +12,25 @@ def balanced_parentheses(parentheses: str) -> bool: >>> """ stack = Stack() + bracket_pairs = {"(": ")", "[": "]", "{": "}"} for bracket in parentheses: if bracket in ("(", "[", "{"): stack.push(bracket) elif bracket in (")", "]", "}"): - if stack.is_empty() or not is_paired(stack.pop(), bracket): + if stack.is_empty() or bracket_pairs[stack.pop()] != bracket: return False return stack.is_empty() -def is_paired(left_bracket: str, right_bracket: str) -> bool: - """ - >>> brackets = {"(" : ")", "[" : "]", "{" : "}"} - >>> for left_bracket, right_bracket in brackets.items(): - ... assert is_paired(left_bracket, right_bracket) - >>> is_paired("(", "}") - False - >>> is_paired("(", "]") - False - """ - return ( - left_bracket == "(" and right_bracket == ")" or - left_bracket == "[" and right_bracket == "]" or - left_bracket == "{" and right_bracket == "}" - ) - - if __name__ == "__main__": from doctest import testmod testmod() - + examples = ["((()))", "((())", "(()))"] print("Balanced parentheses demonstration:\n") for example in examples: print( - example, - "is", + f"{example} is", "balanced" if balanced_parentheses(example) else "not balanced", ) From c78bcbc5ac08b6b05e0613c849933aa5e6a28695 Mon Sep 17 00:00:00 2001 From: shellhub Date: Wed, 28 Oct 2020 17:22:07 +0800 Subject: [PATCH 4/8] remove unused line --- data_structures/stacks/balanced_parentheses.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index c584aeb1b62b..16fa71b563ea 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -9,7 +9,6 @@ def balanced_parentheses(parentheses: str) -> bool: True >>> balanced_parentheses("[(])") False - >>> """ stack = Stack() bracket_pairs = {"(": ")", "[": "]", "{": "}"} From 393e6134a2943ab585a96590b6d40f99e6fb3c5b Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 28 Oct 2020 09:24:28 +0000 Subject: [PATCH 5/8] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index c3b32b1ab754..aece0adcc265 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -520,6 +520,7 @@ * [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/other/sierpinski_triangle.py) * [Tower Of Hanoi](https://github.com/TheAlgorithms/Python/blob/master/other/tower_of_hanoi.py) * [Triplet Sum](https://github.com/TheAlgorithms/Python/blob/master/other/triplet_sum.py) + * [Two Pointer](https://github.com/TheAlgorithms/Python/blob/master/other/two_pointer.py) * [Two Sum](https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py) * [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/other/word_patterns.py) From 030a8f766be3c2baf9b8becb7684d06d8d298507 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Thu, 29 Oct 2020 16:44:12 +0800 Subject: [PATCH 6/8] Update data_structures/stacks/balanced_parentheses.py Co-authored-by: Christian Clauss --- 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 16fa71b563ea..347b97ac534b 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -13,7 +13,7 @@ def balanced_parentheses(parentheses: str) -> bool: stack = Stack() bracket_pairs = {"(": ")", "[": "]", "{": "}"} for bracket in parentheses: - if bracket in ("(", "[", "{"): + if bracket in bracket_pairs: stack.push(bracket) elif bracket in (")", "]", "}"): if stack.is_empty() or bracket_pairs[stack.pop()] != bracket: From 5ba20586ab5dc64ee939807265414d3625d822ec Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Thu, 29 Oct 2020 17:00:07 +0800 Subject: [PATCH 7/8] Add more test cases --- data_structures/stacks/balanced_parentheses.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index 347b97ac534b..4aaabf1471d5 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -9,6 +9,10 @@ def balanced_parentheses(parentheses: str) -> bool: True >>> balanced_parentheses("[(])") False + >>> balanced_parentheses("1+2*3-4") + True + >>> balanced_parentheses("") + True """ stack = Stack() bracket_pairs = {"(": ")", "[": "]", "{": "}"} From e0a5cab53afb9a2e2d185ceb616390f2f5719b24 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Thu, 29 Oct 2020 17:01:41 +0800 Subject: [PATCH 8/8] Update data_structures/stacks/balanced_parentheses.py Co-authored-by: Christian Clauss --- data_structures/stacks/balanced_parentheses.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index 4aaabf1471d5..674f7ea436ed 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -33,7 +33,5 @@ def balanced_parentheses(parentheses: str) -> bool: examples = ["((()))", "((())", "(()))"] print("Balanced parentheses demonstration:\n") for example in examples: - print( - f"{example} is", - "balanced" if balanced_parentheses(example) else "not balanced", - ) + not_str = "" if balanced_parentheses(example) else "not " + print(f"{example} is {not_str}balanced")