Hello all. I'm actually new to python and I also came across of this exercise, checking whether the brackets in a given string are matching or not.
Using various stack commands I've seen many examples. Here, I also attempted a different approach. Anyone give me some suggestion, where I went wrong in the below code?
def matched(str):
ope = []
clo = []
for i in range(0,len(str)):
l = str[i]
if l == "(":
ope = ope + ["("]
else:
if l == ")":
clo = clo + [")"]
else:
return(ope, clo)
if len(ope)==len(clo):
return True
else:
return False
The idea of my code is just to add up "(" & ")" brackets into two different lists, then comparing the length of the list.