Balanced parentheses (#3768)

* Fixed balanced_parentheses.py

* fixed pre-commit

* eliminate is_paired

* remove unused line

* updating DIRECTORY.md

* Update data_structures/stacks/balanced_parentheses.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Add more test cases

* Update data_structures/stacks/balanced_parentheses.py

Co-authored-by: Christian Clauss <cclauss@me.com>

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Du Yuanchao 2020-10-29 17:39:19 +08:00 committed by GitHub
parent a6831c898a
commit a03b3f763f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 12 deletions

View File

@ -520,6 +520,7 @@
* [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/other/sierpinski_triangle.py) * [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) * [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) * [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) * [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) * [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/other/word_patterns.py)

View File

@ -1,23 +1,37 @@
from .stack import Stack from .stack import Stack
__author__ = "Omkar Pathak"
def balanced_parentheses(parentheses: str) -> bool:
def balanced_parentheses(parentheses): """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("([]{})")
stack = Stack(len(parentheses)) True
for parenthesis in parentheses: >>> balanced_parentheses("[()]{}{[()()]()}")
if parenthesis == "(": True
stack.push(parenthesis) >>> balanced_parentheses("[(])")
elif parenthesis == ")": False
if stack.is_empty(): >>> balanced_parentheses("1+2*3-4")
True
>>> balanced_parentheses("")
True
"""
stack = Stack()
bracket_pairs = {"(": ")", "[": "]", "{": "}"}
for bracket in parentheses:
if bracket in bracket_pairs:
stack.push(bracket)
elif bracket in (")", "]", "}"):
if stack.is_empty() or bracket_pairs[stack.pop()] != bracket:
return False return False
stack.pop()
return stack.is_empty() return stack.is_empty()
if __name__ == "__main__": if __name__ == "__main__":
from doctest import testmod
testmod()
examples = ["((()))", "((())", "(()))"] examples = ["((()))", "((())", "(()))"]
print("Balanced parentheses demonstration:\n") print("Balanced parentheses demonstration:\n")
for example in examples: for example in examples:
print(example + ": " + str(balanced_parentheses(example))) not_str = "" if balanced_parentheses(example) else "not "
print(f"{example} is {not_str}balanced")