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).


  • 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.

  • 4
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 10, 2022 at 7:59
  • \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Commented Jun 10, 2022 at 22:14
  • \$\begingroup\$ So, the the the pig the the the is unchanged, since all of the thes are leading and trailing? \$\endgroup\$
    – Neil
    Commented Jun 11, 2022 at 6:16
  • \$\begingroup\$ Also please would you mind explaining the last text case? \$\endgroup\$
    – Neil
    Commented Jun 11, 2022 at 6:18
  • \$\begingroup\$ @Neil The last test case is explicitly explained in the rules. \$\endgroup\$
    – Wheat Wizard
    Commented Jun 11, 2022 at 7:33

6 Answers 6


QuadR, 27 bytes

\b(the )+
 (the .*)

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 .+)

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

≔⪪S θ≔⌕Aθtheη≔⁻η⁺⊕η⟦⁰⊖Lθ⟧η⪫Eθ⁺×№ηκ(ι ⭆η)

Try it online! Link is to verbose version of code. Explanation:

≔⪪S θ

Split the input on spaces.


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:

⪫⪪⪫⪪S the ¦ (the ¦the (the¦the the×)№KA(

Try it online! Link is to verbose version of code. Explanation:

⪫⪪⪫⪪S 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.

Try it online or verify all test cases.


            :  # 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


Try it Online!

Port of Charcoal answer.


Python3, 116 bytes:

import re
f=lambda x,c=0:re.sub(f'(?<{"!="[c]}e) the {["","[^t]"][c]}[\w\W]+',lambda x:f' ({f(x.group()[1:],1)})',x)
  • 1
    \$\begingroup\$ ["","[^t]"][c] => "[^t]"*c \$\endgroup\$
    – naffetS
    Commented Jun 12, 2022 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.