From 7c803e24e1a0138eac218f5ebd384ee850979ad9 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Mon, 1 Sep 2025 04:33:01 +0000 Subject: [PATCH 1/3] [Sync Iteration] python/matching-brackets/3 --- .../matching-brackets/3/matching_brackets.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 solutions/python/matching-brackets/3/matching_brackets.py diff --git a/solutions/python/matching-brackets/3/matching_brackets.py b/solutions/python/matching-brackets/3/matching_brackets.py new file mode 100644 index 0000000..cf0c1a3 --- /dev/null +++ b/solutions/python/matching-brackets/3/matching_brackets.py @@ -0,0 +1,55 @@ +""" +Matching Brackets. + +You're given the opportunity to write software for the Bracketeer™, +an ancient but powerful mainframe. The software that runs on it is +written in a proprietary language. Much of its syntax is familiar, +but you notice lots of brackets, braces and parentheses. Despite +the Bracketeer™ being powerful, it lacks flexibility. If the source +code has any unbalanced brackets, braces or parentheses, the Bracketeer™ +crashes and must be rebooted. To avoid such a scenario, you start writing +code that can verify that brackets, braces, and parentheses are balanced +before attempting to run it on the Bracketeer™. +""" + +PAIRS: dict = { + "]": "[", + "}": "{", + ")": "(", +} + + +def is_paired(input_string: str) -> bool: + """ + Verify that any and all pairs of brackets are matched. + + Given a string containing brackets [], braces {}, parentheses (), + or any combination thereof, verify that any and all pairs are matched + and nested correctly. Any other characters should be ignored. For example, + "{what is (42)}?" is balanced and "[text}" is not. + + :param input_string: + :return: + """ + # Empty string + if not input_string: + return True + + # Remove all non bracket items and convert a string to a list. + brackets_list: list[str] = [ + bracket for bracket in input_string if bracket in "(){}[]" + ] + + # Odd number of brackets + if len(brackets_list) % 2 != 0: + return False + + stack: list[str] = [] + for bracket in brackets_list: + if bracket in "({[": + stack.append(bracket) + elif bracket in PAIRS: + if not stack or stack.pop() != PAIRS[bracket]: + return False + + return not stack From d9155abe7e6641dea50a4884520432082ef604f6 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Sun, 31 Aug 2025 21:36:49 -0700 Subject: [PATCH 2/3] Update matching_brackets.py --- matching-brackets/matching_brackets.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/matching-brackets/matching_brackets.py b/matching-brackets/matching_brackets.py index cf0c1a3..5caea27 100644 --- a/matching-brackets/matching_brackets.py +++ b/matching-brackets/matching_brackets.py @@ -28,8 +28,10 @@ def is_paired(input_string: str) -> bool: and nested correctly. Any other characters should be ignored. For example, "{what is (42)}?" is balanced and "[text}" is not. - :param input_string: - :return: + :param input_string: The string to check for balanced brackets + :type input_string: str + :returns: True if all brackets are properly paired and nested, False otherwise + :rtype: bool """ # Empty string if not input_string: From c3bd1052e67dc9584920ba046d45ea087a00e7b7 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Sun, 31 Aug 2025 21:37:54 -0700 Subject: [PATCH 3/3] Update matching_brackets.py --- matching-brackets/matching_brackets.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matching-brackets/matching_brackets.py b/matching-brackets/matching_brackets.py index 5caea27..ec29936 100644 --- a/matching-brackets/matching_brackets.py +++ b/matching-brackets/matching_brackets.py @@ -30,7 +30,8 @@ def is_paired(input_string: str) -> bool: :param input_string: The string to check for balanced brackets :type input_string: str - :returns: True if all brackets are properly paired and nested, False otherwise + :returns: True if all brackets are properly paired and nested, + False otherwise :rtype: bool """ # Empty string