Your program's input is a string containing whitespaces, parentheses, and other characters. The string is assumed to be parenthesed correctly, i.e. each right parenthesis matches a unique left parenthesis and vice versa : so the program is allowed to do anything on incorrectly parenthesed strings, such as )abc
, (abc
or )abc(
.
The character interval between a left parenthesis and the corresponding right parenthesis (including the parentheses) is called a parenthesed interval.
Your program's job is to decompose the string into blocks according to the rules below, and output the result as a "vertical list", with one block per line.
Now, two characters in the string are in the same block if either
(a) there is no whitespace character strictly between them
or
(b) there is a parenthesed interval containing them both.
Also,
(c) Whitespaces not in any parenthesed interval are mere separators and to be discarded in the decomposition. All the other characters (including whitespaces) are retained.
For example, if the input is
I guess () if(you say ( so)), I'll have(( ))to pack my (((things))) and go
The output should be
I
guess
()
if(you say ( so)),
I'll
have(( ))to
pack
my
(((things)))
and
go
Another useful test (thanks to NinjaBearMonkey) :
Hit the ( Road (Jack Don't) come ) back (no (more ) ) no more
The output should be
Hit
the
( Road (Jack Don't) come )
back
(no (more ) )
no
more
The shortest code in bytes wins.
((foo) bar)
in the test case where all parentheses aren't closed at once. \$\endgroup\$a( b )c( d )e
would also be good. \$\endgroup\$