14
3
Background
Famously, the acronym GNU
stands for GNU's Not Unix
. 1
It's recursive because, after expanding it once, it still contains the acronym GNU
, and so must be exanded again:
(GNU's Not Unix)'s Not Unix
And so on, ad infinitum. Visualizing this, we get a kind of Droste effect:
┌────────────────────────────────────────────┬───────────┐
│┌──────────────────────────────┬───────────┐│'s Not Unix│
││┌────────────────┬───────────┐│'s Not Unix││ │
│││┌──────────────┐│'s Not Unix││ ││ │
││││GNU's Not Unix││ ││ ││ │
│││└──────────────┘│ ││ ││ │
││└────────────────┴───────────┘│ ││ │
│└──────────────────────────────┴───────────┘│ │
└────────────────────────────────────────────┴───────────┘
Recursive acronyms need not recurse on the first word, or only once. For example:
YOPY
:Your Own Personal YOPY
PIPER
:PIPER Is PIPER Expanded Recursively
Visualized:
Challenge
Input
You will be given two inputs:
- A string whose space-delimited words form a recursive acronym. That is, if you form a string from the first letter of each word, that string is guaranteed to be either:
- One of the words of the input string (it may occur more than once).
- A prefix of one or more of those words (e.g.
GNU
is a prefix ofGNU's
) - The casing will match exactly
- A non-negative integer -- the number of times to recursively expand. Given
0
, you'll return the input unaltered (or "framed" once, in its entirety). Given1
, you'll expand once. Etc.
Output
The output is the input string with all instances of the acronym visually expanded, recursively, the specified number of times.
You must use some visual effect to "frame" the nesting -- at minimum, distinct start and end delimiters like parentheses. Ascii boxing of some sort, as in the examples above, is also fine. As would be outputting an actual image that showed the nesting.
I'm flexible as long as the nesting is in fact visualized.
For clarity, parenthesized output would like this:
(((GNU's Not Unix)'s Not Unix)'s Not Unix)'s Not Unix
You are guaranteed that parentheses will never be part of acronym. Other than alphanumeric characters, the acronym will only contain apostrophes, commas, quotes, question marks and exclamation points, and those will only occur as valid punctuation (e.g., a question mark will not appear at the beginning of a word).
This is code golf, fewest bytes wins, no loopholes.
Test Cases
This assumes you're using a parentheses visualization.
Format for test cases:
- Input string (the acronym)
- Input integer (recursion)
- Expected Output
GNU's Not Unix
0
GNU's Not Unix
GNU's Not Unix
2
((GNU's Not Unix)'s Not Unix)'s Not Unix
YOPY Own Personal YOPY
1
(YOPY Own Personal YOPY) Own Personal (YOPY Own Personal YOPY)
YOPY Own Personal YOPY
2
((YOPY Own Personal YOPY) Own Personal (YOPY Own Personal YOPY)) Own Personal ((YOPY Own Personal YOPY) Own Personal (YOPY Own Personal YOPY))
YourYOPY Own Personal YOPY
2
YourYOPY Own Personal (YourYOPY Own Personal (YourYOPY Own Personal YOPY))
40
looks like a duplicate of this challenge :)
– ngn – 2020-02-19T06:55:31.287quotes, question marks and exclamation points. Is
?uestion Mark I For ?MIF?
valid input? – Adám – 2020-02-19T08:44:35.873@Adám That wouldn't be allowed. I added a bit more clarification. It probably would have been simpler to disallow punctuation altogether but I don't see how I can post this challenge without allowing the GNU one, so... – Jonah – 2020-02-19T08:50:01.790
Hmm.. I was hoping your challenge was going to about producing the sort of picture you have under "Visualizing this, we get a kind of Droste effect:". – Anush – 2020-02-19T11:32:30.720
1Next level: visualize mutually recursive acronyms. Eg. “Hurd” → “Hird of Unix-Replacing Daemons” / “Hird” → “Hurd of Interfaces Representing Depth”. – manatwork – 2020-02-19T12:04:32.013
Would
( ( GNU's Not Unix )'s Not Unix )'s Not Unix
be an acceptable output? (note the extra spaces.) – Grimmy – 2020-02-19T12:31:17.2771@Grimmy, Yes, it would. – Jonah – 2020-02-19T15:45:03.550
1@Anush That's absolutely acceptable, even encouraged. Just not required. – Jonah – 2020-02-19T15:53:36.410
2I don't see why we handle
YourYOPY Own Personal YOPY
as if theYOPY
inYourYOPY
is not the acronym - the caps seem to imply it should be :/ – Jonathan Allan – 2020-02-19T18:29:47.873@JonathanAllan It's a reasonable objection. The reasoning is that you'd rarely realistically have an acronym embedded midway like that, but you do have things like
GNU's
, so I made the prefix rule. Then Adam suggested that test case. Though quoted acronyms might be an exception to that. In retrospect, it might have been better to just always expand and avoid special cases altogether. But there's too many answers to make the change now. – Jonah – 2020-02-19T18:36:31.210...well it certainly makes it more challenging! – Jonathan Allan – 2020-02-19T18:45:29.750
...and to be clear what is the desired output for
("ARAS Recurse ARA", 1)
- is it"ARAS Recurse (ARAS Recurse ARA)"
or"(ARAS Recurse ARA)S Recurse (ARAS Recurse ARA)"
? -- i.e. is punctuation special or prefixing? – Jonathan Allan – 2020-02-19T18:51:17.567Both get expanded according to the current rules, which seems like more evidence of their deficiency and the complexity of writing ones we really “want“. Cest la vie. – Jonah – 2020-02-19T18:54:18.893
1@ngn Joined this SE site just to upvote your comment. :D – Rijumone – 2020-02-20T09:06:07.910
haha, thanks and welcome to the site! if you like playing with programming languages, i can assure you there are better reasons to join it than this cheap joke :) – ngn – 2020-02-20T09:49:30.157