Solved

# Need code that adds brackets to multi-leveled expressions (group by indentation levels)

Posted on 2004-04-18

I need a simple and elegant algorithm that will add properly

positioned brackets between 2 or more multi-leveled expressions.

The diagram below shows exactly what I am looking for:

!! NOTE: VIEW IN FIXED FONT SUCH AS COURIER NEW!!

=============================================================

8 POSSIBLE SCENARIOS FOR 3 EXPRESSIONS USING UP TO 2 LEVELS

=============================================================

(1) (2) (3) (4) (5) (6) (7) (8)

+-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+

GROUP LEVEL: |1|2| |1|2| |1|2| |1|2| |1|2| |1|2| |1|2| |1|2|

+-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+

EXPRESION A: |A| | |A| | |A| | |A| | | |A| | |A| | |A| | |A|

+-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+

EXPRESION B: |B| | |B| | | |B| | |B| |B| | |B| | | |B| | |B|

+-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+

EXPRESION C: |C| | | |C| |C| | | |C| |C| | | |C| |C| | | |C|

+-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+

=============================================================

BRACKET SOLUTIONS FOR THE 8 SCENARIOS SHOWN ABOVE

=============================================================

(1) A + B + C

(2) A + B + (C)

(3) A + (B) + C

(4) A + (B + C)

(5) (A) + B + C

(6) (A) + B + (C)

(7) (A + B) + C

(8) (A + B + C)

Things become more complicated as we add more levels and

expressions. Is there an elegant approach?.

Thanks