hi all. i'm trying to write a simple program. i'm using python 3.4

let us say i have

s="2*3+3(8-4(5+6+9))+2+3+3(4/4)"

i want to find expression enclosed in brackets.

i want outputs to be like:
(8-4(5+6+9))
(5+6+9)
(4/4)

i've tried where >>>> denotes an indentation level :

#first part
sbracket=[ ]
ebracket=[ ]
for i in range(len(s)):
>>>>global sbracket
>>>>global ebracket
>>>>if f[i] == "(":
>>>>>>>>sbracket.append(i)
>>>>elif f[i] == ")":
>>>>>>>>ebracket.append(i)

#second part
for i in range(len(sbracket)):
>>>>print(f[sbracket[i]:ebracket[i]])

however, the above code works well as long as there are no nested brackets
like

s="(1+2)(2+3)"

but fails as soon as

s="((3+2))2"

prior to that i wrote it like:
for i in range(len(f)):
>>>>if f[i] == "(":
>>>>>>>>sbracket.append(i)
>>>>>>>>for x in range(len(f)):
>>>>>>>>>>>>if f[i+x]==")":
>>>>>>>>>>>>>>>>ebracket.append(x)
>>>>>>>>>>>>>>>>break

but that too failed to output correctly

note : i'd like an answer involving recursion if possible
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to