2

I am trying to parse a string. Like if this is the string "(A (B (C D) (E F)) (G H))", then I want to parse it like A has children B and G, B has children C and E, and no one else has any children. So, I want the output to be ['A_B_G', 'B_C_E']

I'm doing something like:

lst=[]
str = (A (B (C D) (E F)) (G H))
lst.append(str.split(' '))

And then I'm stuck!

Could somebody give me an idea what to do next?

3
  • 1
    Recursion is a useful way to approach problems like this. Commented Nov 4, 2014 at 13:32
  • 1
    As a fist step, try spitting up the string by seniority Commented Nov 4, 2014 at 13:32
  • 2
    This article solves a similar problem with the use of a stack. Commented Nov 4, 2014 at 13:41

1 Answer 1

1

I'd rather not hint too much, but you can start by converting the list into one like

['(', 'A', '(', 'B', ...]

and then you go over it taking different actions depending on the type of symbol you see e.g. (, ) or a letter.

Sign up to request clarification or add additional context in comments.

1 Comment

I did that. Then what actions should I take and how? Coz I need to find the corresponding closing parentheses for an opening parentheses, how do I do that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.