48
\$\begingroup\$

Anyone remember Boaty?

You could totally make any old word, right?

  • Write a function to turn a string into Somethingy McSomethingface.
  • It should accept one string as input. Ignore the case of the input.
  • If the word ends in 'y', your function should not add an additional 'y' to the first instance, but should remove it in the second instance.
  • If the word ends in 'ey', it should not have an additional 'y' added in the first instance, but should remove both in the second instance.
  • The output should only have upper case letters in the first character, the 'M' of 'Mc' and the first character after 'Mc'.
  • it only needs to work with strings of 3 or more characters.

Examples:

boat                  =>  Boaty McBoatface
Face                  =>  Facey McFaceface
DOG                   =>  Dogy McDogface
Family                =>  Family McFamilface
Lady                  =>  Lady McLadface
Donkey                =>  Donkey McDonkface
Player                =>  Playery McPlayerface
yyy                   =>  Yyy McYyface
DJ Grand Master Flash =>  Dj grand master flashy McDj grand master flashface
\$\endgroup\$
  • \$\begingroup\$ What about spaces in the string, do we leave them intact? Examples: ' y' and ' ' \$\endgroup\$ – touch my body Mar 27 '18 at 17:02
  • 2
    \$\begingroup\$ I’m going to implement a suggestion from @Arnauld and make it three characters minimum. Treat whitespace just like another letter. \$\endgroup\$ – AJFaraday Mar 27 '18 at 17:04
  • \$\begingroup\$ Related: Code Johnny Code, Code! \$\endgroup\$ – Kevin Cruijssen Mar 28 '18 at 9:42
  • \$\begingroup\$ Can we assume the input will only contain upper and lowercase letters? \$\endgroup\$ – Kevin Cruijssen Mar 28 '18 at 13:16
  • \$\begingroup\$ @KevinCruijssen I haven't put any non-letters in the test cases, so they're effectively not concerned. \$\endgroup\$ – AJFaraday Mar 28 '18 at 13:17

37 Answers 37

1
\$\begingroup\$

Python, 105 104

import re;r=re.sub;c=str.capitalize;f=lambda s:r('([^e^y])$','\\1y',c(s))+' Mc'+r('ey$','',c(s))+'face'

thx @Berry M. from noticing my dyslexia

\$\endgroup\$
  • 3
    \$\begingroup\$ -1 byte if you spell Mc correctly! \$\endgroup\$ – Berry M. Mar 29 '18 at 9:45
1
\$\begingroup\$

APL (Dyalog Classic), 95 90 88 87 86 85 84 82 80 78 77 73 bytes

{'face',⍨(' Mc',S↓⍨-+/∧/¨Y'ey'=¨↑∘S¨-⍳2),⍨S,(Y≠⊃⌽S←⍵(819⌶¨)⍨1=⍳⍴⍵)/Y←'y'}

Try it online!

This could be golfed down some more, and any suggestions are welcome!

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

APL (Dyalog Classic), 62 bytes

{'face',⍨(' Mc','e?y$'⎕r''⊢S),⍨S,(Y≠⊃⌽S←⍵(819⌶¨)⍨1=⍳⍴⍵)/Y←'y'}

Try it online!

Thanks to @ngn for this solution!

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

Clojure: 161 155

(use '[clojure.string :as s])
(defn f[t](s/join[(s/capitalize(cond(=(last t)\y)t :else(s/join[t "y"])))" Mc"(s/capitalize(s/replace t #"e*y$" ""))"face"]))

Ungolfed:

(defn facegen [s0]
  (let [ s1 (clojure.string/capitalize (cond
                                         (= (last s0) \y)  s0
                                         :else             (clojure.string/join [s0 "y"])))
         s2 (clojure.string/capitalize (clojure.string/replace s0 #"e*y$" ""))]
    (clojure.string/join [s1 " Mc" s2 "face"])))

Try it online!

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

Unix (79 bytes)

Using Standard input :

 rev|sed -r 's/(.*)(.)$/\L\1\U\2/;s/^(ye?)?(.*)$/ecaF\2cM y\1\2/;s/ yy/ y/'|rev

test :

echo -e "Lady\nFace\nDonkey\nyyy"|rev|sed -r 's/(.*)(.)$/\L\1\U\2/;s/^(ye?)?(.*)$/ecaF\2cM y\1\2/;s/ yy/ y/'|rev
Lady McLadFace
Facey McFaceFace
Donkey McDonkFace
Yyy McYyFace

or ( by Function) :

F(){ echo $1|rev|sed -r 's/(.*)(.)$/\L\1\U\2/;s/^(ye?)?(.*)$/ecaF\2cM y\1\2/;s/ yy/ y/'|rev;}

test :

 F donkey
 Donkey McDonkFace
\$\endgroup\$
1
\$\begingroup\$

PowerShell Core, 98 bytes

(("y$","e?(?=y)y$"|%{$o.ToUpper()[0]+$o.ToLower().Substring(1)-replace $_,""})-join "y Mc")+"face"

Try it online!

Many thanks to @Anderson Pimentel for inspiring a solution using regex replacements!

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

Excel VBA, 134 bytes

An anonymous VBE Immediate Window Function that takes input from range [A1] and outputs to the Immediate Window.

b=[Left(Upper(A1))]+Mid([Lower(A1)],2):l=len(b):y=[Right(A1)="y"]:?b;IIf(y,"","y")" Mc"IIf(y,Left(b,l-1+(InStrRev(b,"e")=l-1)),b)"face
\$\endgroup\$

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.