ファシー・マクフェイスフェイス


47

誰もがボートーを覚えていますか?

古い言葉を完全に作ることができますよね?

  • 文字列をSomethingy McSomethingfaceに変換する関数を作成します。
  • 入力として1つの文字列を受け入れる必要があります。入力の大文字小文字を無視します。
  • 単語が「y」で終わる場合、関数は最初のインスタンスに追加の「y」を追加せず、2番目のインスタンスで削除する必要があります。
  • 単語の末尾が 'ey'の場合、最初のインスタンスで追加の 'y'を追加するのではなく、2番目のインスタンスで両方を削除する必要があります。
  • 出力の最初の文字には大文字、「Mc」の「M」、および「Mc」の後の最初の文字のみを含める必要があります。
  • 3文字以上の文字列を処理する必要があるだけです。

例:

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

文字列内のスペースはどうですか、そのまま残しますか?例:' y'そして' '
私の体に触れる

2
@Arnauldからの提案を実装し、3文字以上にします。空白を別の文字のように扱います。
-AJFaraday


入力に大文字と小文字のみが含まれると仮定できますか?
ケビンCruijssen

@KevinCruijssen私はテストケースに非文字を入れていないので、事実上心配していません。
AJFaraday

回答:


7

スタックス、26 バイト

ëO╛εh╕⌠î&!}∞┌C^U╟«äδ◙Bg⌠└¿

実行してデバッグする

^           convert input to upper case                     "FACE"
B~          chop first character and push it back to input  70 "ACE"
v+          lowercase and concatenate                       "Face"
c'yb        copy, push "y", then copy both                  "Face" "Face" "y" "Face" "y"
:]          string ends with?                               "Face" "Face" "y" 0
T           trim this many character                        "Face" "Face" "y"
+           concatenate                                     "Face" "Facey"
p           output with no newline                          "Face"
"e?y$"z     push some strings                               "Face" "e?y$" ""
" Mc`Rface  execute string template; `R means regex replace " Mc Faceface"
            result is printed because string is unterminated

これを実行する


15

V、27 28 30バイト

Vu~Ùóe¿y$
Hóy$
ÁyJaMc<Esc>Aface

オンラインでお試しください!

<Esc> 表す 0x1b

  • 3文字未満の入力をサポートする必要がないことを学習した後、2バイトをゴルフしました。

  • @DJMcMayhemのおかげで、最初の行の前に2番目の行を操作して1バイトを節約したため、 G

入力はバッファ内にあります。プログラムはすべてを小文字に変換することから始まります

V行を選択してu小文字にします

~ 最初の文字の大文字と小文字を切り替えます(大文字に変換)

そしてÙ一番下の行にカーソルを残し、上記の行を複製

óe¿y$、の圧縮形式e\?y$(オプションey行末にa)を置き換え、何もない(2行目に発生)

H 最初の行に行く

óy$y行末)を最初の行で何も置き換えない

Áy最初の行の最後にa を追加します

J 最後の行を最初の行と中央にスペースを入れて結合し、カーソルをこのスペースに移動します

a追加Mc<Esc>通常モードに戻ります)

A最後faceに、行の最後に追加します



13

Python、144バイト

def f(s):
 s=s[0].upper()+s[1:].lower()
 y=lambda s:s[:-1]if s[-1]=='y'else s
 t=y(s)
 u=s[:-2]if s[-2:]=='ey'else y(s)
 return t+'y Mc%sface'%u

こちらからオンラインでお試しください


2
私の初めてのコードゴルフの試み...
私の体に触れる

3
PPCGへようこそ!オンラインで試すためのリンクを追加することをお勧めします!正当性の検証のために?
ジュゼッペ

1
f("Face")現在のテストケース(TIO)に準拠していません。
ジョナサンフレッチ

正確性のために投稿を編集し、Try It Onlineも追加しました!リンク
私の体に触れる


12

エクセル、204の 144 137 165バイト

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(REPT(REPLACE(LOWER(A1),1,1,UPPER(LEFT(A1)))&"~",2),"~","y Mc",1),"yy ","y "),"ey~","~"),"y~","~"),"~","face")

内側から外側へ:

REPLACE(LOWER(A1),1,1,UPPER(LEFT(A1)))      Replaces PROPER to handle space-delimited cases
REPT(%&"~",2)                   Duplicate.                    Donkey~Donkey~
SUBSTITUTE(%,"~","y Mc",1)      Replace first ~.              Donkeyy McDonkey~
SUBSTITUTE(%,"yy ","y ")        Handle words ending in 'y'.   Donkey McDonkey~
SUBSTITUTE(%,"ey~","~")         Handle words ending in 'ey'   Donkey McDonk~
SUBSTITUTE(%,"y~","~")          Handle words ending in 'y'    Donkey McDonk~
SUBSTITUTE(%,"~","face")        Adding face.                  Donkey McDonkface

古い答え、すべてのビットを個別に作成し、連結します(176バイト)。スペースで区切られたケースを正しく処理しません。

=PROPER(A1)&IF(LOWER(RIGHT(A1,1))="y",,"y")&" Mc"&IF(LOWER(RIGHT(A1,2))="ey",LEFT(PROPER(A1),LEN(A1)-2),IF(LOWER(RIGHT(A1,1))="y",LEFT(PROPER(A1),LEN(A1)-1),PROPER(A1)))&"face"

残念ながら、スペースで区切られたケースを処理する必要があるため、PROPER(A1)無効です(DJ Grand Master Flash入力ケースを参照)。VBAソリューションの作業中に見つけることができる最良の代替品は、LEFT(UPPER(A1))&MID(LOWER(A1),2,LEN(A1))最終的にゴルフをすることになった場合はお知らせください。
テイラースコット

1
@TaylorScottありがとう。2バイト短い「REPLACE(LOWER(A1)、1,1、UPPER(LEFT(A1)))」が見つかりました。
ヴェルニッシュ


9

C#(.NETコア)122 108 139 175 180 179 154バイト

どうもありがとう、リー!

s=>((s.EndsWith("y")?s:s+"y")+" Mc"+(s+"$").Replace("ey$","")+"face").Replace(s,s.ToUpper()[0]+s.Substring(1).ToLower()).Replace("y$","").Replace("$","");

オンラインでお試しください!

C#(.NET Core、LINQを使用)、152バイト

s=>((s.Last()=='y'?s:s+"y")+" Mc"+(s+"$").Replace("ey$","")+"face").Replace(s,s.ToUpper()[0]+s.Substring(1).ToLower()).Replace("y$","").Replace("$","");

オンラインでお試しください!


3
サイトへようこそ!:)
DJMcMayhem


7

ルビー61 49バイト

->s{s.capitalize=~/(e)?y$|$/;"#$`#$1y Mc#$`face"}

オンラインでお試しください!

@MartinEnderのおかげで12バイトを節約できました。


1
私の網膜の答えから正規表現を使用して、文字列の補間をもう少し利用したことは49に、このダウンを取得します。tio.run/##DcxBCsIwEEDRqwxJBF3Y4lpSN0U3igcQwTQmGFptMVNkTOLVY3bvb/...
マーティン・エンダー

@MartinEnderうわー、それはかなりの違いです。括弧なしの文字列補間は見たことがありません。あなたがあなた自身のRubyの答えにそれを使いたくない場合、私はそれを取るでしょう。
iamnotmaynard

いや、それは結構です、を=~使用する代わりに文字列全体を使用して構築することは考えられませんでしたsub。変数がグローバル変数、インスタンス変数、またはクラス変数の場合、文字列補間は角括弧なしで使用できます。
マーティンエンダー

これを使用するには、-pフラグを使用し、次を使用して44 + 1バイトまで減らすことができます。tio.runsub /
Jordan




5

Java 8、121 112 107 106バイト

s->(s=(char)(s.charAt(0)&95)+s.toLowerCase().substring(1)).split("y$")[0]+"y Mc"+s.split("e?y$")[0]+"face"

@OliverGrégoireのおかげで-1バイト。

説明:

オンラインでお試しください。

s->                         // Method with String as both parameter and return-type
  (s=                       //  Replace and return the input with:
     (char)(s.charAt(0)&95) //   The first character of the input as Uppercase
     +s.toLowerCase().substring(1))
                            //   + the rest as lowercase
  .split("y$")[0]           //  Remove single trailing "y" (if present)
  +"y Mc"                   //  Appended with "y Mc"
  +s.split("e?y$")[0]       //  Appended with the modified input, with "y" or "ey" removed
  +"face"                   //  Appended with "face"

最初の文字がアルファベットではない場合はどうなりますか?それとも、私たちはそのことについて追加ルール..得ることができます
streetster

1
@streetsterがOPを尋ねたところ、入力には大文字と小文字のみが含まれているようです。
ケビンクルーッセン

1
~32- > 951バイトの保存のために
オリヴィエ・グレゴワール

OlivierGrégoireI @本当に..>ビット演算についてもう少し学習を開始する必要があります>。
ケビンCruijssen

4

JavaScript、103 96 94バイト

これはかなりナイーブな最初のパスです。

s=>(g=r=>s[0].toUpperCase()+s.slice(1).toLowerCase().split(r)[0])(/y$/)+`y Mc${g(/e?y$/)}face`

オンラインで試す


s =>${s=s[0].toUpperCase()+s.slice(1).toLowerCase().replace(/y$/,``)}y Mc${s.replace(/e?y$/,``)}face
ベンジャミングリュンバウム

1つ少ない:s =>${s=s[0].toUpperCase()+s.slice(1).toLowerCase().replace(/y$/,'')}y Mc${s.replace(/e$/,``)}face
ベンジャミングリュンバウム

@BenjaminGruenbaumに感謝しますが、最初のは失敗しDonkey、2番目は失敗しますFace
シャギー


@Shaggy iは、いくつかの文字でg関数を減らすことができました:)。あなたは私の解決策を見ることができます
-DanielIndie

3

vim、35 34バイト

Vu~Yp:s/ey$
:%s/y$
kgJiy Mc<ESC>Aface<ESC>

<ESC>0x1b

非ゴルフ

Vu~                      # Caseify McCaseface
Yp                       # dup line
:s/ey$ 
:%s/y$                   # Get the suffixes right
kgJiy Mc<ESC>Aface<ESC>  # Join lines and add the extra chars

オンラインでお試しください!

DJMcMayhemのおかげで1バイト節約


1
Y代わりにできることyy
DJMcMayhem

3

Perl 5の -p47の 39バイト

@ OlegV.Volkovの提案で6バイト、@ mwellnhofの提案で1バイト、自分で1バイトを保存しました

$_=lc^$";$_=s/y?$/y Mc/r.s/e?y$//r.face

オンラインでお試しください!


取り除くことができますucfirst$_=lc^$";
オレグV.ボルコフ

$_=s/y$//r."y Mc".s/e?y$//r.face1バイト短くなります。
-nwellnhof

1
/y$|$/->/y?$/
オレグV.ボルコフ

ああ。私はそれに気付いたはずです。
Xcali

3

C ++ 14(G ++)、181の 171 148 147 134バイト

[](auto s){s[0]&=95;int i=1,b;for(;s[i];)s[i++]|=32;b=s[--i]-'y';return s+(b?"y":"")+" Mc"+(b?s:s.substr(0,s[i-1]-'e'?i:i-1))+"face";}

clangはこれをコンパイルしないことに注意してください。

功績はケビン・クルーッセンオリヴィエ・グレゴワールにあり&95ます。

11バイトのゴルフをしてくれたChrisに感謝します。

こちらからオンラインでお試しください。

ゴルフされていないバージョン:

[] (auto s) { // lambda taking an std::string as argument and returning an std::string
    s[0] &= 95; // convert the first character to upper case
    int i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    // i is now s.length()
    b = s[--i] - 'y'; // whether the last character is not a 'y'
    // i is now s.length()-1
    return s + (b ? "y" : "") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[i-1] - 'e' ? i : i-1)) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}

私はC ++についてはよく知りませんが、9バイトをゴルフできます。オンラインで172バイトを試してみてください変更の概要:s[0]=s[0]&~32;からs[0]&=~32;; s[i++]=s[i]|32;s[i++]|=32; そしてint i=1,n=s.length()-1,b;あなたは1つだけを必要としますint
ケビンクルーッセン

ああ、そしてスペースを削除してもう1バイト#include<string>
ケビンクルーッセン

@KevinCruijssenそれをキャッチしてくれてありがとう!編集しました。
OOBalance

whileループni後の値を定義せずに使用するだけで、11バイトを節約できます。オンラインでお試しください!
クリス

@クリスありがとう!あと2バイト削りました。
OOBalance

2

V38 36 32バイト

@Cows quackのおかげで-5バイト

Vu~hy$ó[^y]$/&y
A Mc<esc>póe¿y$
Aface

<esc>リテラルエスケープ文字で[^あり、次のようにエンコードされます\x84

オンラインでお試しください!


gu$になることができますVu
Kritixi Lithos

2
[^は正規表現のショートカットなので(こちらを参照)、[^バイトを保存する代わりに0x84を使用できます。同様に、別のバイトを節約するために\?簡略化でき<M-?>ます。そして$a=>A
Kritixi Lithos


2

Pythonの3117の 114バイト

Dead Possumのおかげで-3バイト

def f(s):s=s.title();return s+'y'*(s[-1]!='y')+' Mc'+([s,s[:-1],0,s[:-2]][(s[-1]=='y')+((s[-2:]=='ey')*2)])+'face'

オンラインでお試しください!


リストの3番目の要素は、1バイトを節約[s,s[:-1],'',s[:-2]するように変更でき0ます。
デッドポッサム

'y'*1 *1必要ありません。さらに2バイト
デッドポッサム

Pythonの3からのPython 2への切り替え、および交換returnでは、print1バイト短いです。
ケビンクルーッセン

2

JavaScript(Node.js)、87バイト

  • 5を減らす5バイトの@Shaggyに感謝
s=>(g=r=>Buffer(s.replace(r,"")).map((x,i)=>i?x|32:x&~32))(/y$/)+`y Mc${g(/e?y$/)}face`

オンラインでお試しください!


2
非再帰関数に名前を付ける必要はありません。
デニス

1
よくできました。使用することは決してないと思いますがBuffer、将来の課題のためにそれを覚えておく必要があります。あなたのために87バイトにそれを得ました。
シャギー

2

K474の 69 68バイト

解決:

{$[r;x;x,"y"]," Mc",_[r:0&1-2/:"ye"=2#|x;x:@[_x;0;.q.upper]],"face"}

例:

q)k)f:{$[r;x;x,"y"]," Mc",_[r:0&1-2/:"ye"=2#|x;x:@[_x;0;.q.upper]],"face"}
q)f each ("boat";"Face";"DOG";"Family";"Lady";"Donkey";"Player")
"Boaty McBoatface"
"Facey McFaceface"
"Dogy McDogface"
"Family McFamilface"
"Lady McLadface"
"Donkey McDonkface"
"Playery McPlayerface"

説明:

最後の文字が等しい場合は"ey"、結果をbase-2に変換して、終了する単語を無視できるようにします"e?"。トリミングする文字数のリストにインデックスを付けます。

私のコードから5バイトを削って、最後の2文字"ey"がうまくいくかどうかを判断しました...

{$[r;x;x,"y"]," Mc",_[r:0&1-2/:"ye"=2#|x;x:@[_x;0;.q.upper]],"face"} / the solution
{                                                                  } / lambda function
                                                            ,"face"  / join with "face"
                    _[                  ;                  ]         / cut function
                                           @[_x; ;        ]          / apply (@) to lowercased input
                                                0                    / at index 0
                                                  .q.upper           / uppercase function
                                         x:                          / save back into x
                                      |x                             / reverse x
                                    2#                               / take first two chars of x
                               "ye"=                                 / equal to "ye"?
                             2/:                                     / convert to base 2
                           1-                                        / subtract from 1
                         0&                                          / and with 0 (take min)
                       r:                                            / save as r
             ," Mc",                                                 / join with " Mc"
 $[r;x;x,"y"]                                                        / join with x (add "y" if required)

ボーナス:

K(oK)の67バイトポート:

{$[r;x;x,"y"]," Mc",((r:0&1-2/"ye"=2#|x)_x:@[_x;0;`c$-32+]),"face"}

オンラインでお試しください!


1
oKポートがK4を無効にした場合、K4のポイントは何ですか?
ザカリー

ASCII値から32を盲目的に減算するため、最初の文字がアルファベット順でない場合、ポートは機能しません。「上位」ビルトインはありません。
ストリートスター

2

ルビー、69バイト

->s{"#{(s.capitalize!||s)[-1]==?y?s:s+?y} Mc#{s.gsub /e?y$/,""}face"}

説明:

->s{                                                                } # lambda 
    "#{                                 } Mc#{                }face" # string interpolation
       (s.capitalize!||s) # returns string capitalized or nil, in that case just use the original string
                         [-1]==?y # if the last character == character literal for y
                                 ?s:s+?y # then s, else s + "y"
                                              s.gsub /e?y$/,"" # global substitute
                                                               # remove "ey" from end

オンラインでお試しください!


TIOリンクを追加できますか?私はRubyを知りませんがs.capitalize、前のものを置き換えsますか?ない場合は、ない/e?y$/で終わるテストケースを扱うYEYまたはEy正しく?
ケビンクルーッセン

1
@KevinCruijssen s.capitalizevs s.capitalize!(異なる機能)。s.capitalize!古いバージョンを壊します。
-dkudriavtsev

@KevinCruijssen TIOリンクを追加しました。
-dkudriavtsev

@KevinCruijssenまた説明を追加
-dkudriavtsev

ああ、説明とについての情報をありがとうs.capitalize!。Rubyでプログラミングすることはありませんが、前の値を置き換えるために説明マークを追加するのはかなりクールです。私から+1。
ケビンクルーッセン

2

Jstx、27 バイト

h</►yT↓►y/◙♂ Mc♀/◄eyg►yg/íå

説明

      # Command line args are automatically loaded onto the stack
h     # Title case the top of the stack
<     # Duplicate the top value on the stack twice
/     # Print the top value on the stack
►y    # Load 'y' onto the stack
T     # Returns true if the 2nd element on the stack ends with the top
↓     # Execute block if the top of the stack is false
  ►y  # Load 'y' onto the stack
  /   # Print the top value on the stack
◙     # End the conditional block
♂ Mc♀ # Load ' Mc' onto the stack
/     # Print the top value on the stack
◄ey   # Load 'ey' onto the stack
g     # Delete the top of the stack from the end of the 2nd element on the stack if it exists
►y    # Load 'y' onto the stack
g     # Delete the top of the stack from the end of the 2nd element on the stack if it exists
/     # Print the top of the stack
íå    # Load 'face' onto the stack
      # Print with newline is implied as the program exits

オンラインでお試しください!


この言語を見たことがありません。おもしろそうです。ドキュメントはありますか?
再帰的


うわー、これは本当に印象的です。特に非常に短い開発時間。これがどこに行くのか楽しみです。
再帰的

2

143142バイト

func[s][s: lowercase s s/1: uppercase s/1
w: copy s if"y"<> last s[append w"y"]rejoin[w" Mc"parse s[collect keep to[opt["y"|"ey"]end]]"face"]]

オンラインでお試しください!

ゴルフをしていない:

f: func[s][
   s: lowercase s                      ; make the entire string lowercase
   s/1: uppercase s/1                  ; raise only its first symbol to uppercase 
   w: copy s                           ; save a copy of it to w
   if "y" <> last s[append w "y"]     ; append 'y' to w if it doesn't have one at its end
   rejoin[w                            ; assemble the result by joining:
          " Mc"
          ; keep the string until "y", "ey" or its end
          parse s[collect keep to [opt ["y" | "ey"] end]]
          "face"
    ]
]

2

PHP:132

<?php function f($s){$s=ucfirst(strtolower($s));return $s.(substr($s,-1)=='y'?'':'y').' Mc'.preg_replace('/(ey|y)$/','',$s).'face';}

説明:

<?php

function f($s)
{
    // Take the string, make it all lowercase, then make the first character uppercase
    $s = ucfirst(strtolower($s));

    // Return the string, followed by a 'y' if not already at the end, then ' Mc'
    // and the string again (this time, removing 'y' or 'ey' at the end), then
    // finally tacking on 'face'.
    return $s
        . (substr($s, -1) == 'y' ? '' : 'y')
        . ' Mc'
        . preg_replace('/(ey|y)$/', '', $s)
        . 'face';
}


2

Pyth、 36 34バイト

++Jrz4*\yqJK:J"e?y$"k+" Mc"+K"face

オンラインでお試しください!

説明:

++Jrz4*\yqJK:J"(e)?y$"k+" Mc"+K"face

  Jrz4                                  Set J to the titlecase of z (input)
           K:J"e?y$"k                   Set K to (replace all matches of the regex e?y$ in J with k (empty string))
         qJ                             Compare if equal to J
      *\y                               Multiply by "y" (if True, aka if no matches, this gives "y", else it gives "")
 +                                      Concatenate (with J)
                             +K"face    Concatenate K with "face"
                       +" Mc"           Concatenate " Mc" with that
+                                       Concatenate

悲しいことに、最後のテストケースが失敗するため、これは機能しません。
ザカリー

に切り替えrz3rz4、これを最後のテストケースで適切に動作するようにします。
hakr14

ああ、私はそれを修正します:P
RK。

2

エリキシル112の 110 107 106バイト

今ではJavaと同じくらい短い

fn x->x=String.capitalize x;"#{x<>if x=~~r/y$/,do: "",else: "y"} Mc#{String.replace x,~r/e?y$/,""}face"end

オンラインでお試しください!

説明:

x=String.capitalize x

x最初の文字を大文字で取得し、他のすべての文字を小文字で取得します。

#{ code }

コードを評価し、文字列に挿入します。

#{x<>if x=~ ~r/y$/, do: "", else: "y"}

xy末尾にない場合y(つまり、正規表現に一致しない場合)に連結しy$ます。

#{String.replace x, ~r/e?y$/, "")}

末尾eyと末尾を削除しますy



1

Pyth、60 59バイトSBCS

K"ey"Jrz4Iq>2JK=<2J=kK.?=k\yIqeJk=<1J))%." s÷   WZÞàQ"[JkJ

テストスイート

彼らはここに表示されませんが、3つのバイトは、\x9c\x82、と\x8cの間にパックされた文字列であるs÷。リンクにはそれらが含まれていますのでご安心ください。

Python 3の翻訳:
K="ey"
J=input().capitalize()
if J[-2:]==K:
    J=J[:-2]
    k=K
else:
    k="y"
    if J[-1]==k:
        J=J[:-1]
print("{}{} Mc{}face".format(J,k,J))
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.