未验证 提交 a03b3f76 编写于 作者: ishellhub's avatar ishellhub 提交者: GitHub

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: NChristian Clauss <cclauss@me.com>

* Add more test cases

* Update data_structures/stacks/balanced_parentheses.py
Co-authored-by: NChristian Clauss <cclauss@me.com>
Co-authored-by: Ngithub-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: NChristian Clauss <cclauss@me.com>
上级 a6831c89
...@@ -520,6 +520,7 @@ ...@@ -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)
......
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")
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册