Suppose we use the following rules to pull a single string from another string, one containing only ASCII printable characters and called an *-string. If the string runs out before the process halts, that is an error, and the result of the process is undefined in that case:
- Start with
d=1, s="" - Whenever you encounter a
*, multiplydby 2. Whenever you encounter another character, concatenate it to the end ofsand subtract 1 fromd. If nowd=0, halt and returns
Defined Examples:
d->d
769->7
abcd56->a
*abcd56->ab
**abcd56->abcd
*7*690->769
***abcdefghij->abcdefgh
Undefined Examples: (note that the empty string would be one of these as well)
*7
**769
*7*
*a*b
*
Your job is to take a string and return the shortest *-string that produces that string.
Program Examples:
7->7
a->a
ab->*ab
abcd->**abcd
769->*7*69
Your program should handle any string containing at least one character and only non-* ASCII printable characters. You can never return strings for which the process is undefined, since by definition they cannot produce ANY strings.
Standard loopholes and I/O rules apply.
*? \$\endgroup\$