We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 62cfd18 commit 3555bf9Copy full SHA for 3555bf9
Parentheses_Matching.py
@@ -0,0 +1,26 @@
1
+# Python3 code to Check for balanced parentheses in an expression
2
+open_list = ["[","{","("]
3
+close_list = ["]","}",")"]
4
+
5
+# Function to check parentheses
6
+def check(myStr):
7
+ stack = []
8
+ for i in myStr:
9
+ if i in open_list:
10
+ stack.append(i)
11
+ elif i in close_list:
12
+ pos = close_list.index(i)
13
+ if ((len(stack) > 0) and
14
+ (open_list[pos] == stack[len(stack)-1])):
15
+ stack.pop()
16
+ else:
17
+ return "Unbalanced"
18
+ if len(stack) == 0:
19
+ return "Balanced"
20
21
+# Driver code
22
+string = "{[]{()}}"
23
+print(string,"-", check(string))
24
25
+string = "[{}{})(]"
26
0 commit comments