Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Solved #91 Added : balanced Parenthesis #95

Merged
merged 5 commits into from
Aug 16, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Basic-Scripts/balanced_paranthesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
openBracketList = ["[", "{", "("]
closeBracketList = ["]", "}", ")"]


def checkParentheses(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,
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 Unbalanced.
"""
stack = []
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"
if len(stack) == 0:
return "Balanced"
else:
return "Unbalanced"


if __name__ == "__main__":

data = input("Enter the string to check:\t")
result = checkParentheses(data)
print(result)