Description
Given a formula,
we assign to every parenthesis a number called 'depth'
which equals to the number of unbalanced left-parenthesis to the left of it.
For a left-parenthesis, the number counts it.
In the following example, the first line is a formula,
and the depth of every parenthesis is shown in the second line:
func1(func2(x) + y) + func3(z)
1 2 1 0 1 0
The job is inserting a newline character after every right-parenthesis of
depth 0.
|
| Raw Input
| func1(x) + func2(y) + func3(z)
func1(func2(x) + y) + func3(z)
func1(x) + func2(func3(y) + func4(z)) + func5(w)
|
|
| Desired Output
| func1(x)
+ func2(y)
+ func3(z)
func1(func2(x) + y)
+ func3(z)
func1(x)
+ func2(func3(y) + func4(z))
+ func5(w)
|
|
Script and Comments
Script1 [ 1] /\n/!s/^.*/\n&\n/
[ 2] :loop
[ 3] s/\n\([^()][^()]*\)\(.*\n\)/\1\n\2/
[ 4] s/\n(\(.*\n\)/(\n\1#/
[ 5] s/\n)\(.*\n\)#/)\n\1/
[ 6] /\n..*\n./b loop
[ 7] P
[ 8] /\n\n/d
[ 9] s/\n$//
[10] D
| |
|