私の本当の友達のためにゴルフをコーディングする


35

...そして私のコード友達のための本当のゴルフ。

これは、1年前のXKCDコミックに基づいたチャレンジでほとんどがトースト(コミックの最初のパロディ)から成り立っていますが、わずかなバリエーションがあります。

あなたの仕事は、friends(標準入力または関数引数を介して)入力としてコミックからトーストの前半(すべてを含む)を取得し、正確なテキストを出力(または返す)するプログラム(または関数)を書くことです後半。無効な入力を処理する必要はありません。標準の抜け穴が適用され、バイト単位の最短コードが優先されます。

他の場所からトーストをコピーしたくない人を助けるために、ここにすべてを入力と出力に分けて示します。

Input: Champagne for my real friends
Output: and real pain for my sham friends!

Input: Pseudopods for my real friends
Output: and real pods for my pseudo-friends!

Input: Petticoats for my real friends
Output: and real coats for my petty friends.

Input: Loosestrife for my real friends
Output: and real strife for my loose friends!

Input: Ladybugs for my real friends
Output: and real bugs for my lady friends!

Input: Single-payer for my real friends
Output: and RealPlayer for my single friends.

Input: Tumbleweeds for my real friends
Output: and real weed for my Tumblr friends!

Input: Fauxhawks for my real friends
Output: and real hawks for my faux friends!

Input: Platonic solids for my real friends
Output: and real solids for my platonic friends!

Single-payer代わりにタイプミスがあると思いますSingle-player
ケビンCruijssen

4
ケビン:いいえSingle-payer
ivzem

@KevinCruijssen-タイプミスかもしれませんが、そうであれば、ivzemではなくRandall @ XKCDのものです。Explain xkcdでそれに関する議論があります。また、ivzem-HNQリストでタイトルを見るとすぐに、あなたはその漫画を参照していると確信しました。よくやった!
ボブソン

回答:


18

網膜、119バイト

以前のバージョンでは、「プラトニックソリッド」のスペースを正しく管理していませんでしたが、これは機能し、短くなっています:)

ew
rw
eds
ed
(\w+) ?([^oatr ]\w{3}.+)real
and real $2$1
C
S
gne
in
o 
o-
ti
ty
T`TL`Tl
p\w+y.+
$&.
s$
s!
real -p
RealPl

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

これにより、一連の置換により入力が出力に変換されます。

最も興味深い部分は、この置換(正規表現のゴルフの種類)です。

(\w+) ?([^oatr ]\w{3}.+)real
and real $2$1

最初の単語を分割し、そのピースを適切な場所に配置し、余分なスペースを削除して、出力の構造を構築することにより、ほぼすべての作業を行います。「Tumbleweeds」テストケースで正しく機能するためには、これは以前の代替「eds」->「ed」に依存します。

残りのほとんどは、特殊なケースを処理する置換によって構成されます。他の興味深い部分は次のとおりです。

T`TL`Tl

これにより、 "T"(Tumblrの場合)を除くすべてが小文字になります。

p\w+y.+
$&.
s$
s!

これにより、「。」が配置されます。「p」の後に「y」が付いた単語を含む各文の終わり(実際には「小物」と「支払人」)。次に、「!」を配置します 「s」で終わるすべての文の最後(その他すべて)。


これはの後に余分なスペースを出力しplatonicます。推奨される修正:TIO(+5バイト)
数学中毒

1
@math_junkieありがとう、スペースの問題を修正し、さらに短くなった新しいバージョンをアップロードしました:)
Leo

10

Pythonの2、291の 269 293 255 247バイト

Erik the Outgolfer22バイトの節約に感謝します!

終わるいくつかの出力のためのアカウントに24バイト.の代わりに、!

lambda x:'and '+['real '+'pain%ssham ,pods%spseudo-,coats%spetty ,strife%sloose ,bugs%slady ,weed%sTumblr ,hawks%sfaux ,solids%splatonic '.split(',')['noarsekc'.find(x[7])],'RealPlayer%ssingle ']['-'in x]%' for my '+x[-7:]+'!.'['-'in x or'tt'in x]

物事を開始するためのシンプルなソリューション。コメントで提案されているように、入力の8文字目をチェックし、配列の辞書で対応する出力を検索します。

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


あなたは忘れているかもしれませんSingle-payer for my real friends...
マチューRodic

@MathieuRodicいいえ、それは非常に意図的なものです。RealPlayerは特別な場合です
数学中毒者

申し訳ありませんが、速すぎて読みません。
マチューロディック

6

SOGL、143 バイト

_╔x⅜²‘,8WWAa"⅞+1Tλ²⅞ƨ◄…χŗbdŗu8ņ∑Ι⅓I‼hzΔμō┘═q‼xΘ▼²ηpG⅔─┌¡↕+wd÷[≈┐α┌ļ○(‚δΦEΤα{‚φ▒k׀:╚s&⅛↑»‘ |Θwθ1w"ρ└⁸‘S∆∫⁴‘a1>*+oo"¤Ε○Φr‘o2w _@ŗo"æn‘o"χ}49⁶‘aWT

これは、この文字列をメインパーツとして使用します。「|」スプリッターであり、「_」はスペースプレースホルダーであるため、分割は正しく機能します。

RealPlayer single_|weed Tumblr_|strife loose_|bugs lady_|pods pseudo-|pain sham_|solids platonic_|coats petty_|hawks faux_

入力例:「私の本当の友達のためのプラトニックソリッド」説明:

..‘,8WWAa"..‘ |Θwθ1w"..‘S∆∫⁴‘a1>*+oo"..‘o2w _@ŗo"..‘o"..‘aWT  strings shortened
..‘                                                           push "personcake" (the indexing string) ["personcake"]
    8W                                                        get the 8-th char                       
   ,                                                           from the input                         ["personcake", "c"]
      W                                                       get its in "personcake" (1-indexed)     [7]
       Aa                                                     save on variable A                      [7]
         "..‘                                                 push the long string                    [7, "RealPlayer...faux_"]
              |Θ                                              split on "|"                            [7, ["RealPlayer single_", ..., "hawks faux_"]]
                w                                             get the Ath item of the array           [["..."], "solids platonic_"]
                 θ                                            split on spaces                         [["..."], ["solids", "platonic_"]]
                  1w                                          get the 1st item                        [["..."], ["solids", "platonic_"], "solids"]
                    "..‘                                      push "and "                             [["..."], ["solids", "platonic_"], "solids", "and "]
                        S∆∫⁴‘                                 push "real "                            [["..."], ["solids", "platonic_"], "solids", "and ", "real "]
                             a1>*                             multiply by A(index) > 1                [["..."], ["solids", "platonic_"], "solids", "and ", "real "]
                                 +                            join together                           [["..."], ["solids", "platonic_"], "solids", "and real "]
                                  o                           output                                  [["..."], ["solids", "platonic_"], "solids"]
                                   o                          output (the 1st item of the array)      [["..."], ["solids", "platonic_"]]
                                    "..‘o                     output " for my "                       [["..."], ["solids", "platonic_"]]
                                         2w                   get the 2nd item of the array           [["..."], ["solids", "platonic_"], "platonic_"]
                                            _@ŗ               replace "_" with " "                    [["..."], ["solids", "platonic_"], "platonic "]
                                               o              output that                             [["..."], ["solids", "platonic_"]]
                                                "..‘o         output "friends"                        [["..."], ["solids", "platonic_"]]
                                                     "..‘     push ".!!!!!!.!"                        [["..."], ["solids", "platonic_"], ".!!!!!!.!"]
                                                         aW   get the Ath item                        [["..."], ["solids", "platonic_"], "!"]
                                                           T  output, disabling implicit output       [["..."], ["solids", "platonic_"]]

6

Pythonの3788403359の 396バイト

最新バージョン

これは今では私の5回目の試みです。私は元のプログラムの半分のサイズに管理しました。現在、欠落している「-」が含まれており、完全なソリューションであると考えています。それでも私はかさばる側に疑いがあります。しかし、目標にはるかに近い。私は多くの助けを受けました。すべての有用なガイダンスをありがとう。

s=input()
x=0
l= "Ch,pain,Sham,Ps,pods,psuedo-,Pe,coats,petty,Lo,strife,loose,La,bugs,lady,Si,RealPlayer,single,Tu,weed,Tumblr,Fa,hawks,faux,Pl,solids,platonic".split(",")
a,b,c,d,e,f,g,h = " and real","for my","friends",".!","print(a,l[p+1],b,l[p+2]",",c+d[0])","+c+d[1])",",c+d[1])"
for p in range(0,27,3):
 x+=1
 if s[:2] == l[p]:
  if x == 2: eval(e+g)
  else: eval(e+f if x in(3,6) else e+h)

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


元の投稿

これはコードゴルフに関する私の最初の投稿なので、私の不器用なプログラムをおadvanceびします。単語を解析して「シャンパン」を「痛み」、「偽物」に変換するプログラムを作成する方法がわかりません。しかし、私は他の誰かがそれを解決するのを見たいです。ですから、私の能力のレベルは、私のプログラムが「シャンパン」が「痛み」、「偽物」であることを事前に知る必要があることを示しているので、入力要求を実際にコーディングすることはほとんど意味がありません。その結果、私はそれを省き、印刷出力で少し文字通りになりました。大丈夫だと思います:)

phraseList= [   ("Champagne","pain","Sham"), 
                ("Psudeopods","pods","psuedo-"), 
                ("Petticoats","coats","petty"),
                ("Loosestrife","strife","loose"),
                ("Ladybugs","bugs","lady"),
                ("Single-payer","coats","petty"),
                ("Petticoats","RealPlayer","single"),
                ("Tumbleweeds","weed","Tumblr"),
                ("Fauxhawks","hawks","faux"),
                ("Platonic solids","real solids","platonic")
                ]

for phrase in phraseList:
    print("Input: ",phrase[0], "for my real friends.")
    if "-" in phrase[2]:
        print("Output: and real", phrase[1], "for my", phrase[2]+ "friends!")
    else:
        print("Output: and real", phrase[1], "for my", phrase[2], "friends!")

3
ヘルプセンターから課題に対するすべての解決策は次のとおりです。たとえば、コードゴルフコンテストへの参加はゴルフをする必要があり、スピードコンテストへの参加は速くなるようにいくつかの試みを行う必要があります。
エリックアウトゴルファー

1
こんにちは、サイトへようこそ。これはコードとゴルフの質問であり、コードの長さを最小限に抑えるよう努力する必要があります。不要な空白を削除し、変数の名前を1文字に変更する必要があります。
小麦ウィザード

phraseList全体を1行に入れ、インデントを1スペースに減らし、ifとelseを使用して印刷を1行にまとめることができます。印刷の空白は必要ありません。とにかく、すべての出力を印刷することが許可されているかどうかはわかりません;)
メガマン

1
こんにちは!これはあなたの最初のコードゴルフの答えのようです。残念ながら、質問、特に下部の「例」の表を誤解しているようです。この表は、各入力をそれぞれの出力にマッピングする方法を示しています。現在、あなたのプログラムはそのテーブルを印刷するだけです。 Q&Aサイトというよりも競争のようです。_ウィートウィザードはすでにそれを行っていたので、それは良いです
ivzem

こんにちは、PPCGへようこそ。スペースを削除して1文字の変数を使用すると、大量のバイトを失う可能性があります。私がここで始めたとき、慣れるのに時間がかかったことを覚えています。あなたは私のようです。読み取り可能なコードを書くのに多くの時間を費やします。私が言ったように、PPCGと新しい世界へようこそ;)
ElPedro

6

ローダ299 292 288 259バイト

文では,なく@fergusqを使用して4バイト節約..push

@fergusqのおかげでバイトが節約されました。 indexOf

h a{r=indexOf(a[7:8],"noarspekc")A="pain>pods>coats>strife>Player>bugs>weed>hawks>solids>sham >pseudo->petty >loose >lady >single >Tumblr >faux >platonic >!>!>.>!>!>.>!>!>!"/">";["and "];["Real"]if[r=4]else["real "];[A[r]," for my ",A[r+9],"friends",A[r+18]]}

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

Pythonに非常に近い...非常に近い... あなたの動き、Python。

説明

h a{
  r=indexOf(a[7:8],"noarspekc")  /*Gets the index of the character in this string*/
  /*Variable A contains all the unique words for each test case*/
  A="pain>pods>coats>strife>Player>bugs>weed>hawks>solids>sham >pseudo->petty >loose >lady >single >Tumblr >faux >platonic >!>!>.>!>!>.>!>!>!"/">"
  ["and "]
  ["Real"]if[r=4]else["real "]   /*RealPlayer*/
  [A[r]," for my ",A[r+9],"friends",A[r+18]]   /*Print everything using var A*/
}

最後のステートメントで文字列補間を使用して1バイトを保存できますか?編集:実際には、最短の方法はおそらく1つではなく多くの文字列をストリームにプッシュすることです。置き換える..,
-fergusq

@fergusqヒントをありがとう!
クリチキシリトス

また、単に使用することもできますr=indexOf(a[7:8],"noarspekc")
fergusq

@fergusqああ、私はそのような組み込みが存在することを知りませんでした。ありがとう!
クリチキシリソス

5

JavaScript(ES6)、230 228 221 216バイト

s=>'and '+((n='paonrsekc'.search(s[7]))?'real ':'')+'RealPl740s5 /450p3y /540p5-/pain0s3 /460l4 /340l3 /540T4r /350f3 /860p7 '.split`/`[n].replace(/\d\d?/g,n=>s.substr(n/10+1,n%10)||' for my ')+'friends'+'.!'[+(n>1)]

テスト


3

PHP、202 220 204 203バイト

and real <?=[pods,solids,hawks,strife,RealPlayer,pain,bugs,weed,coats][$q=md5($argv[1].LnI)%9].' for my '.[pseudo,platonic,faux,loose,single,sham,lady,Tumblr,petty][$q].' -'[!$q].friends.'.!'[!$q||$q&3];

3

Perl、173 168バイト

改行とインデントを削除すると、これは173バイトのPerl5コードになります。LeoのRetina回答から最初の正規表現を恥知らずに盗みました。(鉱山は数文字長くなりました)

sub f{
  my($_)=@_;
  s,(\S+[oieyxm ])(\S{4}.+)real,and real $2\l$1,;
  s,gne,in,;
  s,ch,sh,;
  s,ti,ty,;
  s,eds,ed,;
  s,tumble,Tumblr,;
  s,real -p,RealPl,;
  s,o ,o-,;
  s,c ,c,;
  /ng|tt/?"$_.":"$_!"
}

perl5バージョン> = 5.14の場合、evalと新しい/ r正規表現置換修飾子を使用して、さらに5バイトを削ることができます。168バイトで終わる:

sub f{my($_)=@_;eval's,(\S+[oieyxm ])(\S{4}.+)real,and real $2\l$1Xgne,inXch,shXti,tyXeds,edXtumble,TumblrXreal -p,RealPlXo ,o-Xc ,c,;/ng|tt/?"$_.":"$_!"'=~s/X/,;s,/gr}

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


2

C、367バイト

思ったほど短くなりませんでした。

i,j,c;f(char*s){c=s[7]-97;char t[9],u[200];char*r=s;s+=j=c&&c-17?c-10&&c-18?c-2?5:8:3:4;for(i=0;*++s-32;t[i++]=*s);t[c-4?i:i-1]=0;for(i=0;*r++-t[0];)u[i++]=*(r-1);u[i]=32;u[i+1]=0;u[0]+=32;u[c?i:i-1]=c?c-2?c-14?32:45:0:121;printf("and %s%s for my %sfriends%c",c-15?"real ":"RealPlayer",c-15?c-13?t:"pain":"",c-13?c-4?c-17?u:"loose ":"Tumblr ":"sham ",c&&c-15?33:46);}

2

Java 7、585 553バイト

import java.util.*;String c(String s){Map m=new HashMap(){{put("Ch","pain");put("Ps","pods");put("Pe","coats");put("Lo","strife");put("La","bugs");put("Si","Player");put("Tu","weed");put("Fa","hawks");put("Pl","solids");put("Ch1","sham ");put("Ps1","pseudo-");put("Pe1","petty ");put("Lo1","loose ");put("La1","lady ");put("Si1","single ");put("Tu1","Tumblr ");put("Fa1","faux ");put("Pl1","platonic ");}};String r=s.substring(0,2);int c=r.charAt(1);return"and "+(c=='i'?"Real":"real ")+m.get(r)+" for my "+m.get(r+1)+"friends"+(c=='i'|c=='e'?'.':'!');}

@Zirconのおかげで-32バイト。

地図とは異なるものを使用することで間違いなくゴルフを楽しむことができます。

説明:

import java.util.*;            // Import required for the Map & HashMap
String c(String s){            // Method with String parameter and String return-type
  Map m=new HashMap(){{        //  The Map
    put("Ch","pain");put("Ps","pods");put("Pe","coats");put("Lo","strife");put("La","bugs");put("Si","Player");put("Tu","weed");put("Fa","hawks");
                               //  Add mapping from first two characters with first word
    put("Ch1","sham ");put("Ps1","pseudo-");put("Pe1","petty ");put("Lo1","loose ");put("La1","lady ");put("Si1","single ");put("Tu1","Tumblr ");put("Fa1","faux ");put("Pl1","platonic ");
                               //  Add mapping from first two characters + "1" with second word (+ space or '-' for edge-case `pseudo-friends`)
  }};                          // End of Map initialization
  String r=s.substring(0,2);   //  Get the first two characters of the input String
  int c=r.charAt(1);           //  Get the second character
  return "and "                //  return "and "
    + (c=='i'?"Real":"real ")  //   + "Real" or "real " for edge-case `RealPlayers`
    + m.get(r)                 //   + first word from Map
    + " for my "               //   + " for my "
    + m.get(r+1)               //   + second word from Map
    + "friends"                //   + "friends"
    + (c=='i'|c=='e' ? '.'     //   + '.' for edge-cases 'Petticoats' and 'Single-player
      : '!');                  //     or '!' for all other cases
}                              // End of method

テストコード:

ここで試してみてください。(ideone.comは最近バグがありますので、今はTIOを使用しています。)

import java.util.*;
class M{
  static String c(String s){Map m=new HashMap();m.put("Ch","pain");m.put("Ps","pods");m.put("Pe","coats");m.put("Lo","strife");m.put("La","bugs");m.put("Si","Player");m.put("Tu","weed");m.put("Fa","hawks");m.put("Pl","solids");m.put("Ch1","sham ");m.put("Ps1","pseudo-");m.put("Pe1","petty ");m.put("Lo1","loose ");m.put("La1","lady ");m.put("Si1","single ");m.put("Tu1","Tumblr ");m.put("Fa1","faux ");m.put("Pl1","platonic ");String r=s.substring(0,2);int c=r.charAt(1);return"and "+(c=='i'?"Real":"real ")+m.get(r)+" for my "+m.get(r+1)+"friends"+(c=='i'|c=='e'?'.':'!');}

  public static void main(String[] a){
    System.out.println(c("Champagne for my real friends"));
    System.out.println(c("Pseudopods for my real friends"));
    System.out.println(c("Petticoats for my real friends"));
    System.out.println(c("Loosestrife for my real friends"));
    System.out.println(c("Ladybugs for my real friends"));
    System.out.println(c("Single-player for my real friends"));
    System.out.println(c("Tumbleweeds for my real friends"));
    System.out.println(c("Fauxhawks for my real friends"));
    System.out.println(c("Platonic solids for my real friends"));
  }
}

1
put代わりに二重括弧の初期化を使用すると、バイトを節約できると思いますm.put
ジルコン

2

C(gcc)、311バイト

パターンをクリアしますが、非常に多くの例外があります。それでも、これよりも良い方法があるはずです!

f(char*s){char*t="\"5$#(=931",*r[]={"pain", "sham","pods","pseudo","coats","petty","strife","loose","bugs","lady","Player","single","weed","Tumblr","hawks","faux","solids","platonic"};int i=strchr(t,*s^s[2])-t;printf("and %s%s for my %s%cfriends%c",i^5?"real ":"Real",r[i*2],r[i*2+1],i^1?32:45,i^5&&i^2?33:46);}

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

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.