Write a program that takes in (via stdin or command line) a string with the recursive form
PREFIX[SUFFIXES]
where
PREFIX
may be any string of lowercase letters (a-z), including the empty string, andSUFFIXES
may be any sequence of strings with the recursive formPREFIX[SUFFIXES]
concatenated together, including the empty sequence.
Generate a list of lowercase lettered strings from the input by recursively evaluating the list of strings in each of the suffixes and appending them to the prefix. Output to stdout the strings in this list in any order, one per line (plus an optional trailing newline).
Example
If the input is
cat[s[up[][]][]ch[e[r[]s[]]]a[maran[]comb[]pult[[]ing[]]]]
then the prefix is
cat
and and the suffixes ares[up[][]]
,[]
,ch[e[r[]s[]]]
, anda[maran[]comb[]pult[[]ing[]]]
. Each suffix has its own prefix and suffixes in turn.The output would be these 9 words in any order
catsup cats cat catcher catches catamaran catacomb catapult catapulting
because the input encodes this tree
and each of the 9 output words can be formed by traversing the tree from root to leaf.
Notes
Remember that the prefix may be the empty string, so something like
[donut[][]cruller[]]
is valid input whose output would be (in any order)
donut cruller
where the empty line is for the empty string that the second suffix matches.
The suffix sequence can also be empty, so the trivial input case
[]
has a single empty line as its output:
- You may assume that the input will only produce unique output words.
- e.g.
hat[s[]ter[]s[]]
would be invalid input becausehats
is encoded twice. - Similarly,
[[][]]
is invalid because the empty string is encoded twice.
- e.g.
- You may not assume that the input is as short or compressed as possible.
- e.g. the
'e'
node in the main example above could be combined with the'ch'
node, but that's doesn't mean the input is invalid. - Similarly,
[[[[[]]]]]
is valid, despite only encoding the empty string in a sub-optimal way.
- e.g. the
- Instead of a program you may write a function that takes the input string as an argument and prints the output normally or returns it as a string or list.
The shortest code in bytes wins.