From ce20c840d99397e3a8939ed653f8200380ec4f77 Mon Sep 17 00:00:00 2001 From: sameer srivastava <56088741+sameersrivastava13@users.noreply.github.com> Date: Fri, 14 Aug 2020 14:29:50 +0530 Subject: [PATCH 1/5] Solved #91 Added : balanced Parenthesis --- Basic-Scripts/balanced_paranthesis.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Basic-Scripts/balanced_paranthesis.py diff --git a/Basic-Scripts/balanced_paranthesis.py b/Basic-Scripts/balanced_paranthesis.py new file mode 100644 index 00000000..f08af455 --- /dev/null +++ b/Basic-Scripts/balanced_paranthesis.py @@ -0,0 +1,26 @@ +open_bracket_list = ["[", "{", "("] +close_bracket_list = ["]", "}", ")"] + + +def checkParanthsis(data): + stack = [] + for i in data: + if i in open_bracket_list: + stack.append(i) + elif i in close_bracket_list: + pos = close_bracket_list.index(i) + if (len(stack) > 0) and (open_bracket_list[pos] == stack[len(stack) - 1]): + stack.pop() + else: + return "Unbalanced" + if len(stack) == 0: + return "Balanced" + else: + return "Unbalanced" + + +if __name__ == "__main__": + + data = input() + result = checkParanthsis(data) + print(result) From 5a1a5a94802f82a57dde2e590d38548dd405923c Mon Sep 17 00:00:00 2001 From: sameer srivastava <56088741+sameersrivastava13@users.noreply.github.com> Date: Sat, 15 Aug 2020 11:13:48 +0530 Subject: [PATCH 2/5] Refactored : balanced_paranthsis.py --- Basic-Scripts/balanced_paranthesis.py | 31 +++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/Basic-Scripts/balanced_paranthesis.py b/Basic-Scripts/balanced_paranthesis.py index f08af455..421d30c4 100644 --- a/Basic-Scripts/balanced_paranthesis.py +++ b/Basic-Scripts/balanced_paranthesis.py @@ -1,15 +1,28 @@ -open_bracket_list = ["[", "{", "("] -close_bracket_list = ["]", "}", ")"] +openBracketList = ["[", "{", "("] +closeBracketList = ["]", "}", ")"] -def checkParanthsis(data): +def checkParanthsis(data: str) -> str: + """ + checkParanthsis() : Will take a string as an arguement and each time when an open parentheses is encountered + will push it in the stack, and when closed parenthesis is encountered, + will match it with the top of stack and pop it. + + Parameters: + data (str): takes a string. + + Returns: + str: Returns a string value whether string passed is balanced or Unblanaced. + """ stack = [] - for i in data: - if i in open_bracket_list: - stack.append(i) - elif i in close_bracket_list: - pos = close_bracket_list.index(i) - if (len(stack) > 0) and (open_bracket_list[pos] == stack[len(stack) - 1]): + for index in data: + if index in openBracketList: + stack.append(index) + elif index in closeBracketList: + position = closeBracketList.index(index) + if (len(stack) > 0) and ( + openBracketList[position] == stack[len(stack) - 1] + ): stack.pop() else: return "Unbalanced" From 59a1f2f75ef7be1c28498e5e356dc930a67aefb1 Mon Sep 17 00:00:00 2001 From: sameer srivastava <56088741+sameersrivastava13@users.noreply.github.com> Date: Sat, 15 Aug 2020 15:45:38 +0530 Subject: [PATCH 3/5] Refactored : balanced_parentheses.py --- Basic-Scripts/balanced_paranthesis.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Basic-Scripts/balanced_paranthesis.py b/Basic-Scripts/balanced_paranthesis.py index 421d30c4..ecd7e5a2 100644 --- a/Basic-Scripts/balanced_paranthesis.py +++ b/Basic-Scripts/balanced_paranthesis.py @@ -2,9 +2,9 @@ closeBracketList = ["]", "}", ")"] -def checkParanthsis(data: str) -> str: +def checkParentheses(data: str) -> str: """ - checkParanthsis() : Will take a string as an arguement and each time when an open parentheses is encountered + checkParentheses() : Will take a string as an arguement and each time when an open parentheses is encountered will push it in the stack, and when closed parenthesis is encountered, will match it with the top of stack and pop it. @@ -12,7 +12,7 @@ def checkParanthsis(data: str) -> str: data (str): takes a string. Returns: - str: Returns a string value whether string passed is balanced or Unblanaced. + str: Returns a string value whether string passed is balanced or Unbalanced. """ stack = [] for index in data: @@ -34,6 +34,7 @@ def checkParanthsis(data: str) -> str: if __name__ == "__main__": - data = input() - result = checkParanthsis(data) + data = input("Enter the string to check:\t") + result = checkParentheses(data) print(result) + From adb16b589e4c0469d52428300fedc8c662081a9a Mon Sep 17 00:00:00 2001 From: sameer srivastava <56088741+sameersrivastava13@users.noreply.github.com> Date: Sun, 16 Aug 2020 11:06:21 +0530 Subject: [PATCH 4/5] Solved #91 refactored : balanced Parenthesis --- Basic-Scripts/balanced_paranthesis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Basic-Scripts/balanced_paranthesis.py b/Basic-Scripts/balanced_paranthesis.py index ecd7e5a2..f27df8b4 100644 --- a/Basic-Scripts/balanced_paranthesis.py +++ b/Basic-Scripts/balanced_paranthesis.py @@ -2,7 +2,7 @@ closeBracketList = ["]", "}", ")"] -def checkParentheses(data: str) -> str: +def check_parentheses(data: str) -> str: """ checkParentheses() : Will take a string as an arguement and each time when an open parentheses is encountered will push it in the stack, and when closed parenthesis is encountered, From 9d69518335e168f1be43f9bb636db7b79979fd50 Mon Sep 17 00:00:00 2001 From: sameer srivastava <56088741+sameersrivastava13@users.noreply.github.com> Date: Sun, 16 Aug 2020 11:07:51 +0530 Subject: [PATCH 5/5] Solved #91 Added : balanced Parentheses --- Basic-Scripts/balanced_paranthesis.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Basic-Scripts/balanced_paranthesis.py b/Basic-Scripts/balanced_paranthesis.py index f27df8b4..7709f1b8 100644 --- a/Basic-Scripts/balanced_paranthesis.py +++ b/Basic-Scripts/balanced_paranthesis.py @@ -4,7 +4,7 @@ def check_parentheses(data: str) -> str: """ - checkParentheses() : Will take a string as an arguement and each time when an open parentheses is encountered + check_parentheses() : Will take a string as an arguement and each time when an open parentheses is encountered will push it in the stack, and when closed parenthesis is encountered, will match it with the top of stack and pop it. @@ -35,6 +35,6 @@ def check_parentheses(data: str) -> str: if __name__ == "__main__": data = input("Enter the string to check:\t") - result = checkParentheses(data) + result = check_parentheses(data) print(result)