10
\$\begingroup\$

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.

\$\endgroup\$
20
  • 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\$ Jun 10 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
    Jun 10 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
    Jun 11 at 6:16
  • \$\begingroup\$ Also please would you mind explaining the last text case? \$\endgroup\$
    – Neil
    Jun 11 at 6:18
  • \$\begingroup\$ @Neil The last test case is explicitly explained in the rules. \$\endgroup\$
    – Wheat Wizard
    Jun 11 at 7:33

6 Answers 6

4
\$\begingroup\$

QuadR, 27 bytes

\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

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
0
2
\$\begingroup\$

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.

≔⌕Aθtheη

Find all the indices of the.

≔⁻η⁺⊕η⟦⁰⊖Lθ⟧η

Remove any index that's 1 more than another index, and 0 and the last index (because those thes weren't surrounded by spaces).

⪫Eθ⁺×№ηκ(ι 

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.

×)№KA(

Output enough )s to match the number of (s.

\$\endgroup\$
0
1
\$\begingroup\$

05AB1E, 31 bytes

„ €€ÀDć'(©«ì:…€€ (8∍D®K:D®¢')׫

Port of @Steffan's Python answer.

Try it online or verify all test cases.

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 (".

\$\endgroup\$
1
\$\begingroup\$

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!

Port of Charcoal answer.

\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ ["","[^t]"][c] => "[^t]"*c \$\endgroup\$
    – Steffan
    Jun 12 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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