Skip to content

Commit 9f877bd

Browse files
authored
Update Check redundant brackets.py
1 parent b70d052 commit 9f877bd

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Check redundant brackets/Check redundant brackets.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ def checkRedundantBrackets(expression) :
3636
le = len(s)
3737
l=[]
3838
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
5152

5253
expression=input("Enter the expression:\n")
5354
print("output:")

0 commit comments

Comments
 (0)