File tree 1 file changed +42
-0
lines changed
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ from create_stack import Stack
2
+
3
+
4
+ def is_match (p1 , p2 ):
5
+ if p1 == "(" and p2 == ")" :
6
+ return True
7
+ elif p1 == "{" and p2 == "}" :
8
+ return True
9
+ elif p1 == "[" and p2 == "]" :
10
+ return True
11
+ else :
12
+ return False
13
+
14
+
15
+ def is_paren_balanced (paren_string ):
16
+ s = Stack ()
17
+ is_balanced = True
18
+ index = 0
19
+
20
+ while index < len (paren_string ) and is_balanced :
21
+ paren = paren_string [index ]
22
+ if paren in "([{" :
23
+ s .push (paren )
24
+ else :
25
+ if s .is_empty ():
26
+ is_balanced = False
27
+ else :
28
+ top = s .pop ()
29
+ if not is_match (top , paren ):
30
+ is_balanced = False
31
+ index += 1
32
+
33
+ if s .is_empty () and is_balanced :
34
+ return True
35
+ else :
36
+ return False
37
+
38
+
39
+ print (is_paren_balanced ("(((({}))))" ))
40
+
41
+ print (is_paren_balanced ("[][]]]" ))
42
+ print (is_paren_balanced ("[][]" ))
You can’t perform that action at this time.
0 commit comments