25
Given a properly-parenthesized string as input, output a list of all nonempty substrings within matching parentheses (or outside of all parentheses), with nested parentheses removed. Each substring should be the sequence of characters in exactly the same matching parentheses. Substrings should be listed in order of depth, and substrings of the same depth should be listed in the order they occur in the string. Assume the input is always correctly parenthesized.
You may assume that the input contains only lower-case ASCII letters and parentheses.
Your answer should be a function that, when given a string, returns a list of strings.
Examples:
'a(b)c(d)e' -> ['ace', 'b', 'd']
'a(b(c)d)e' -> ['ae', 'bd', 'c']
'a((((b))))' -> ['a', 'b']
'a()b' -> ['ab']
'' -> []
'a' -> ['a']
'(((a(b)c(d)e)f)g)h' -> ['h', 'g', 'f', 'ace', 'b', 'd']
'ab(c(((d)ef()g)h()(i)j)kl)()' -> ['ab', 'ckl', 'hj', 'efg', 'i', 'd']
Fewest bytes wins.
Are
'i'
and'd'
in the correct order in the last test case? – PurkkaKoodari – 2015-11-16T08:24:50.427@Pietu1998
i
is less deeply nested thand
. – feersum – 2015-11-16T08:31:01.580@feersum Oh, right. – PurkkaKoodari – 2015-11-16T08:31:57.857
1
Would you mind allowing the other standard submission types as well, in particular full programs? Not all languages have a concept of functions. For default consensus see http://meta.codegolf.stackexchange.com/a/2422/8478 and http://meta.codegolf.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods.
– Martin Ender – 2015-11-16T20:17:37.537@MartinBüttner Sure. I'm pretty new to code golf; I didn't realize that was an issue. – redstonerodent – 2015-11-16T20:43:16.973
2@redstonerodent The wording I tend to use is "You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter." and in your case "The output may be in any convenient, unambiguous, flat list format." – Martin Ender – 2015-11-16T20:46:27.810