This repository was archived by the owner on Nov 30, 2022. It is now read-only.
File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments