File tree Expand file tree Collapse file tree 1 file changed +13
-12
lines changed Expand file tree Collapse file tree 1 file changed +13
-12
lines changed Original file line number Diff line number Diff line change @@ -36,18 +36,19 @@ def checkRedundantBrackets(expression) :
36
36
le = len (s )
37
37
l = []
38
38
for i in range (le ):
39
- if s [i ]== "(" :
40
- l .append ("(" )
41
- elif s [i ] in "+-*/" :
42
- l .append (s [i ])
43
- elif s [i ] == ")" :
44
- if l [- 1 ]== "(" :
45
- return True
46
- elif l [- 1 ] in "+-*/" :
47
- while l [- 1 ]!= "(" :
48
- l .pop ()
49
- l .pop ()
50
- return False
39
+ if s [i ]== "(" : # checks open bracket
40
+ l .append ("(" ) # put open brackets to stack
41
+ elif s [i ] in "+-*/" : #check char in the string
42
+ l .append (s [i ]) #if it an operator then put it into the stack
43
+ elif s [i ] == ")" : # checks close bracket
44
+ if l [- 1 ]== "(" : # and if there is no operator in the stack
45
+ return True # then its redundant and further code will not run
46
+ elif l [- 1 ] in "+-*/" : # if it has operator
47
+
48
+ while l [- 1 ]!= "(" : # then pop till we don't get open bracket
49
+ l .pop ()
50
+ l .pop () #pop open bracket
51
+ return False #it is false if no redundant bracket is found till end
51
52
52
53
expression = input ("Enter the expression:\n " )
53
54
print ("output:" )
You can’t perform that action at this time.
0 commit comments