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

Commit a1cb98a

Browse files
authored
Merge pull request #95 from sameersrivastava13/master
Solved #91 Added : balanced Parenthesis
2 parents 6d59af5 + 9d69518 commit a1cb98a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Basic-Scripts/balanced_paranthesis.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
openBracketList = ["[", "{", "("]
2+
closeBracketList = ["]", "}", ")"]
3+
4+
5+
def check_parentheses(data: str) -> str:
6+
"""
7+
check_parentheses() : Will take a string as an arguement and each time when an open parentheses is encountered
8+
will push it in the stack, and when closed parenthesis is encountered,
9+
will match it with the top of stack and pop it.
10+
11+
Parameters:
12+
data (str): takes a string.
13+
14+
Returns:
15+
str: Returns a string value whether string passed is balanced or Unbalanced.
16+
"""
17+
stack = []
18+
for index in data:
19+
if index in openBracketList:
20+
stack.append(index)
21+
elif index in closeBracketList:
22+
position = closeBracketList.index(index)
23+
if (len(stack) > 0) and (
24+
openBracketList[position] == stack[len(stack) - 1]
25+
):
26+
stack.pop()
27+
else:
28+
return "Unbalanced"
29+
if len(stack) == 0:
30+
return "Balanced"
31+
else:
32+
return "Unbalanced"
33+
34+
35+
if __name__ == "__main__":
36+
37+
data = input("Enter the string to check:\t")
38+
result = check_parentheses(data)
39+
print(result)
40+

0 commit comments

Comments
 (0)