Write a program that takes in (via stdin or command line) a string with the recursive form
PREFIXmay be any string of lowercase letters (a-z), including the empty string, and
SUFFIXESmay be any sequence of strings with the recursive form
PREFIX[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).
If the input is
then the prefix is
catand and the suffixes are
a[marancombpult[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.
Remember that the prefix may be the empty string, so something like
is valid input whose output would be (in any order)
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.
hat[sters]would be invalid input because
hatsis encoded twice.
[]is invalid because the empty string is encoded twice.
- 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.
[[[[]]]]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.