为我真正的朋友打高尔夫球


35

...以及我的代码朋友真正的高尔夫。

这是一个以一岁的XKCD漫画为基础的挑战,该漫画主要由吐司(漫画中第一个漫画的模仿者)组成,遵循不同的模式,但略有不同。

您的任务是编写一个程序(或函数),以将漫画中任何吐司的上半部分(包括在内的所有内容friends)作为输入(通过stdin或函数参数)并输出(或返回)下半场。您不必处理无效输入,适用标准漏洞,以字节为单位的最短代码获胜。

例子

为了帮助那些不想从任何其他地方复制吐司的人,这里的所有内容都分为输入和输出。

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)。
Kevin Cruijssen

4
凯文:不。确实是Single-payer
ivzem

@KevinCruijssen-可能是拼写错误,但如果是这样,那是Randall @ XKCD的问题,不是ivzem。在Explain xkcd上有关于它的讨论。另外,ivzem-当我在HNQ列表中看到标题时,我确定您正在引用该漫画。做得好!
Bobson

Answers:


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

通过拆分第一个单词,将其各个单词放置在正确的位置,删除多余的空格,并构建输出的结构,几乎可以完成所有工作。要在“风滚草”测试用例上正常工作,这取决于先前的替换“ eds”->“ ed”。

其余部分主要由处理特殊情况的替换组成。其他有趣的部分是:

T`TL`Tl

这会将除“ T”(对于Tumblr)之外的所有内容都转换为小写。

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

这放置一个“。” 在每个句子的末尾包含一个带有“ y”的单词,在“ p”之后加上一些字母(实际上是“小额”和“付款人”)。然后放置一个“!” 在所有以“ s”结尾的句子的末尾(所有其他)。


这会在之后打印多余的空格platonic。建议的修正:TIO(+5字节)
数学迷

1
@math_junkie谢谢,我上传了一个新版本,该版本解决了空格问题,甚至更短:)
Leo

10

Python 2,291 269 293 255 247字节

感谢您Erik the Outgolfer节省22个字节!

+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]

从头开始的简单解决方案。按照注释中的建议检查输入的第八个字母,并在字典的数组中查找相应的输出。

在线尝试!


您可能已经忘记了Single-payer for my real friends……
Mathieu Rodic

@MathieuRodic不,那是故意的。RealPlayer是一个特例
数学迷

糟糕,抱歉,阅读速度太快。
Mathieu Rodic

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 3中788403359个 396字节

最新版本

这是我的第五次尝试。我已经设法缩小了原来程序的一半大小。现在它包括缺少的“-”,我相信这是一个完整的解决方案。我仍然怀疑体积庞大。但更接近目标。我有很多帮助。感谢所有有用的指导。

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)

在线尝试!


原始过帐

这是我的第一篇关于代码高尔夫的文章,因此请为我的笨拙程序提前道歉。无法看到如何通过解析单词将“香槟”转换为“疼痛”,“假”的程序。但是我希望看到其他人解决这个问题。因此,由于我的能力水平要求我的程序需要事先知道“香槟”是“痛苦”,“虚假”,因此实际上编码输入请求似乎没有意义。结果,我忽略了它,并在打印输出中使用了一些文字。希望没事:)

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
从帮助中心所有的解决方案,以挑战应:[...]是在使用中获奖标准的有力竞争者。例如,参加代码高尔夫比赛的条目需要打高尔夫球,而参加速度比赛的条目应该做出一些快速的尝试。
暴民埃里克(Erik the Outgolfer)

1
您好,欢迎来到该网站。这是一个代码问题,需要您努力使代码长度最小化。您应该删除不必要的空格并将变量重命名为1个字符长。
小麦巫师

您可以将整个短语列表放在一行中,将缩进量减少到一个空格,将打印内容与if和else放在一行中,并且不需要打印内容中的空白。无论如何,我不知道您是否只允许打印所有输出;)
Mega Man

1
你好!看来这是您的第一个高尔夫答案代码。不幸的是,您似乎误解了这个问题,尤其是底部的“示例”表,该表仅显示了每个输入应如何映射到各自的输出。目前,您的程序只打印该表。 _小麦向导已经做到了,所以更好
ivzem

嗨,欢迎来到PPCG。删除空格并使用单字母变量可能会丢失很多字节。我记得我刚从这里开始的时候花了一些时间来习惯。我猜你就像我一样。您花费大量时间编写可读代码。正如我所说,欢迎来到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 ......如此接近.... 你我的行动,巨蟒。

说明

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*/
}

您可以在最后一条语句中使用字符串插值来保存一个字节吗?编辑:实际上,最短的方法可能是将许多字符串而不是一个推入流中。替换..,
fergusq

@fergusq感谢您的提示!
Kritixi Lithos

您也可以使用r=indexOf(a[7:8],"noarspekc")
fergusq

@fergusq啊,我不知道存在这样的内置函数。谢谢!
Kritixi Lithos

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,173168字节

除去换行符和缩进,这将成为Perl5代码的173个字节。从Leo的视网膜答案中无耻地偷走了第一个正则表达式。(我的长了几个字符)

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/?"$_.":"$_!"
}

对于> = 5.14的perl5版本,可以使用eval和新的/ r regexp替换修饰符将另外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'?'.':'!');}

-32个字节,感谢@Zircon

绝对可以使用不同于地图的东西打高尔夫球。

说明:

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
Zircon

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);}

在线尝试!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.