# Parenthetical names

Your challenge is to build a program that puts parentheses around the 'title' part of names. For example, Sonic the Hedgehog is converted to Sonic (the Hedgehog).

### Rules:

• Around every the that passes the following rules, an opening parentheses will be added before the the and a corresponding closing parentheses will be added at the end of the string.
• Inputs will only contain words (uppercase/lowercase letters and spaces).
• Trailing thes are ignored.
• With more than one thes in a row, every other the is parenthesized. (So pig the the the hog hog hog turns into pig (the the (the hog hog hog)).)
• Remaining thes are skipped unless surrounded by spaces. For example, the the in their would not be parenthesized.

#### I know this may seem lacking in detail. However, it's hard to get one set of really specific guidelines working- just follow the above rules and you're good.

Inspired by this xkcd comic: https://xkcd.com/2252/

This is code-golf, so least number of bytes wins.

• Since it hasn't been said yet: we have the Sandbox of proposed challenges where challenges can be posted, reviewed, adjusted, and perfected based on feedback before it goes to main. Oh, and welcome to CGCC of course. :) If you clarify all the comments and add rules/test cases for them, the challenge can be reopened. As mentioned by @pxeger in the comment above: a challenge should be clear from the rules alone, and shouldn't be deducted/guessed based on test cases. Jun 10 at 7:59
• This seems clear to me now. I've reopened it. However a bunch of the answers are probably invalid. If anyone wants to take it upon themselves to figure out which and inform their authors be my guest. Invalid answers will be subject to deletion. Jun 10 at 22:14
• So, the the the pig the the the is unchanged, since all of the thes are leading and trailing?
– Neil
Jun 11 at 6:16
• Also please would you mind explaining the last text case?
– Neil
Jun 11 at 6:18
• @Neil The last test case is explicitly explained in the rules. Jun 11 at 7:33

\b(the )+
(the .*)
&
(\1)


Try it online!

\b(the )+ replace multiple thes with:
& themselves

 (the .*) replace a space followed by the and everything until the end with:
 (\1) a space followed by the parenthesised text

# Retina 0.8.2, 27 bytes

+(?<!\bthe) (the .+)
($1)  Try it online! Link includes test cases. Explanation: Parenthesises from all  the s to the end of the line, unless the  the  follows a previous the. # Charcoal, 40 bytes ≔⪪Ｓ θ≔⌕Ａθtheη≔⁻η⁺⊕η⟦⁰⊖Ｌθ⟧η⪫Ｅθ⁺×№ηκ(ι ⭆η)  Try it online! Link is to verbose version of code. Explanation: ≔⪪Ｓ θ  Split the input on spaces. ≔⌕Ａθtheη  Find all the indices of the. ≔⁻η⁺⊕η⟦⁰⊖Ｌθ⟧η  Remove any index that's 1 more than another index, and 0 and the last index (because those thes weren't surrounded by spaces). ⪫Ｅθ⁺×№ηκ(ι  Prefix ( to the remaining indices and join with spaces. ⭆η)  Output the matching )s at the end. A port of @Steffan's Python answer is also 40 bytes: ⪫⪪⪫⪪Ｓ the ¦ (the ¦the (the¦the the×)№ＫＡ(  Try it online! Link is to verbose version of code. Explanation: ⪫⪪⪫⪪Ｓ the ¦ (the ¦the (the¦the the  Substitute  the  with  (the  and the (the with the the. ×)№ＫＡ(  Output enough )s to match the number of (s. # 05AB1E, 31 bytes „ €€ÀDć'(©«ì:…€€ (8∍D®K:D®¢')×«  Port of @Steffan's Python answer. Explanation:  : # Replace all „ €€À # " the ": „ €€ # Push dictionary string " the" À # Rotate it once towards the right: " the " Dć'(©«ì '# with " (the ": D # Duplicate the " the " ć # Extract the head '( '# Push "(" © # Store it in variable ® (without popping) « # Append it to the extracted space ì # Merge it in front of the remaining string: " (the " : # Then replace all …€€ (8∍ # "the (the": …€€ ( # Push dictionary string "the (" 8∍ # Extend it to size 8: "the (the" D®K # with "the the": D # Duplicate the "the (the" ®K # Remove the ® ("("): "the the" D # Duplicate the resulting string ®¢ # Pop the copy, and count the amount of ® ("(") in it ')× '# Pop and push a string with that many ")" « # Append it at the end of the string # (after which the result is output implicitly)  See this 05AB1E tip of mine (section How to use the dictionary?) to understand why „ €€ is " the" and …€€ ( is "the (". # Python, 93 bytes lambda s,t='the',r=' (',w=' ':(p:=s.replace(w+t+w,r+t+w).replace(t+r+t,t+w+t))+p.count(r)*')' Attempt This Online! # Vyxal, 31 bytes ⌈D‛λλÞIf$L‹o:›Fꜝλ$$p;VṄ:\(O$$*J


Try it Online!

import re

• ["","[^t]"][c] => "[^t]"*c` Jun 12 at 14:45