北极熊,北极熊,您听到了什么?


50

儿童读物《北极熊》,《北极熊》,您听到了什么? 有一个非常简单的模式。您可以使用本文来教入门编程概念。

我的儿子很快就对这个想法感到无聊,所以我决定去打高尔夫。

挑战

用您选择的编程语言,编写尽可能小的程序,该程序将打印以下文本。输出应完全匹配,包括大小写,空格和标点符号。

Polar Bear, Polar Bear, what do you hear?
I hear a lion roaring in my ear.

Lion, Lion, what do you hear?
I hear a hippopotamus snorting in my ear.

Hippopotamus, Hippopotamus, what do you hear?
I hear a flamingo fluting in my ear.

Flamingo, Flamingo, what do you hear?
I hear a zebra braying in my ear.

Zebra, Zebra, what do you hear?
I hear a boa constrictor hissing in my ear.

Boa Constrictor, Boa Constrictor, what do you hear?
I hear an elephant trumpeting in my ear.

Elephant, Elephant, what do you hear?
I hear a leopard snarling in my ear.

Leopard, Leopard, what do you hear?
I hear a peacock yelping in my ear.

Peacock, Peacock, what do you hear?
I hear a walrus bellowing in my ear.

Walrus, Walrus, what do you hear?
I hear a zookeeper whistling in my ear.

Zookeeper, Zookeeper, what do you hear?
I hear children...

...growling like a polar bear,
roaring like a lion,
snorting like a hippopotamus,
fluting like a flamingo,
braying like a zebra,
hissing like a boa constrictor,
trumpeting like an elephant,
snarling like a leopard,
yelping like a peacock,
bellowing like a walrus...
that's what I hear.

6
kolmogorov-complexity将是一个不错的选择
charredgrass

3
是大写B,并CPolar BearBoa Constrictor必要吗?(分批处理将花费我10%的字节数...)
Neil

是否可以提交哈希值并强制使用受限制的解决方案?:D
polym

4
这是一个非常好的第一个挑战!
Alex A.

1
我会+1,但您的投票数是42,而我又是谁来干扰银河系呢?
FantaC

Answers:


34

05AB1E,349个 331 330 322 314 311字节

•4i;kV_†©:š×Îj€Yå—‚‘%˜ESδþ¤çÑ9¶‹{Å€7¾à{Ì®qA•35B0¡…ing«lV•7ü[¿‘¢wÀ¶à-‚¤î„jHâ™ÐJ'µ‡ÀÂý6›ü‚š¸€%NtÅýµL›fU¥ì€€uîT¡›Ÿ{!œ>'Ì&ý§¨Ü?é>•36B1ð:0¡™D©„, «D‚ø“€À€·€î—«?“¶«¸â€˜€JU¦“„¾“‚˜lv„I “—«0€†€¯¶å.“«0¡`yð«Y¦õ‚˜Nè«sr„ aN5Qi'n«}ð«N9›ijrj¨ðs«…...«}srJˆ}X¯‚ø€Jvy,¶?}…...DU?Y¨vN__i',¶}yð“€è€…“N6Qi'n«}ð®NèJl?}X,“€Š's€À I—«.“?

在线尝试

说明

•4i;kV_†©:š×Îj€Yå—‚‘%˜ESδþ¤çÑ9¶‹{Å€7¾à{Ì®qA•
包含声音的打包字符串 GROWL0ROAR0SNORT0FLUT0BRAY0HISS0TRUMPET0SNARL0YELP0BELLOW0WHISTL

35B0¡…ing«lV
解压缩,拆分为0,为每个单词添加“ ing”,转换为小写并存储在变量Y中

•7ü[¿‘¢wÀ¶à-‚¤î„jHâ™ÐJ'µ‡ÀÂý6›ü‚š¸€%NtÅýµL›fU¥ì€€uîT¡›Ÿ{!œ>'Ì&ý§¨Ü?é>•
包含动物的包装绳子 POLAR1BEAR0LION0HIPPOPOTAMUS0FLAMINGO0ZEBRA0BOA1CONSTRICTOR0ELEPHANT0LEOPARD0PEACOCK0WALRUS0ZOOKEEPER

36B1ð:0¡™
打开包装,将1替换为空格,分割为0,然后转换为标题大小写

D©„, «D‚ø
复制,保存以供以后使用,将“,”添加到一个副本中,然后复制并压缩。

“€À€·€î—«?“¶«¸
词典单词“您听到了什么?”,后接新行,添加到新列表中

‘€JU
将“动物,动物”和“您听到的声音?\ n”列表的笛卡尔积进行展平并加入并将其存储在变量X中。将格式为“动物,动物,您听到的声音”的字符串列表放入列表中”。

¦“„¾“‚˜l
从以前的剩余动物列表中删除动物管理员并添加孩子,将其转换为小写。

v
对于每只动物:

„I “—«0€†€¯¶å.“«0¡`

按下“我在耳朵里听到0”,在0上分割并按单独的字符串插入。

yð«Y¦õ‚˜Nè«
向没有声音的孩子推“动物的声音”

sr„ a
将“ a”推入正确的位置

N5Qi'n«}
如果动物是“大象”,请添加“ n”

ð«
添加空间。

N9›ijrj¨ðs«…...«}
如果动物是孩子,请删除空间并添加“ ...”

srJˆ}
加上句子“我在耳朵里听到动物的声音”。(以及子项),存储在全局数组和结束循环中

X¯‚ø€J
检索前几行,压缩并加入,生成韵律的第一部分“动物,动物,您会听到什么?\ n我听到了动物的声音”。

vy,¶?}
打印后跟换行。

…...DU?
打印“ ...”并将副本存储在变量X中。

Y¨v
对于除“呼啸”之外的每种声音:

N__i',¶}对于除第一行以外的每一行,均以“,\ n”开头(使逗号位于上一行)

yð“€è€…“
推送声音,空间和“像”

N6Qi'n«}
如果Elephant添加“ n”。

ð®NèJl?}X,
检索保存在寄存器中的动物,加入所有内容,打印并结束循环,后跟“ ...”。这会产生“听起来像动物的声音”线条。

“€Š's€À I—«.“?
打印字典字符串“这就是我所听到的”。


30
这是召唤克苏鲁。
aloisdg说恢复莫妮卡

6
我看不到代码的结尾和打包数据的开始。
亚当

4
@Adám我知道。它看起来很像胡言乱语:)我最终会添加一个解释,希望可以使它更清晰。我可能想先尝试一下。
Emigna '16

28
这可能是外行如何想象代码。
noɥʇʎԀʎzɐɹƆ

1
我不知道我刚刚读过什么...
Nico 2016年

9

PHP,420个 414 434 414 412字节

从CLI调用或添加<pre>到输出。

$v=[growl,roar,snort,flut,bray,hiss,trumpet,snarl,yelp,bellow,whistl];$a[6]=n;foreach($n=['polar bear',lion,hippopotamus,flamingo,zebra,'boa constrictor',elephant,leopard,peacock,walrus,zookeeper]as$i=>$p)echo$i?"I hear a$a[$i] $p $v[$i]ing in my ear.

":"",$q=ucwords($p),", $q, what do you hear?
",$i>9?"I hear children...

...":""&$z[]="$v[$i]ing like a$a[$i] $p";echo join(",
",$z),"...
that's what I hear.";
  • 关于未定义常量的一些注意事项;PHP隐式转换为文字字符串的行为再次出现
  • 为“大象”添加“ n”需要20个字节... hmpf。
  • 通过重新排序东西得到了20个字节:)

分解

$n=['polar bear',lion,hippopotamus,flamingo,zebra,'boa constrictor',elephant,leopard,peacock,walrus,zookeeper];
$v=[growl,roar,snort,flut,bray,hiss,trumpet,snarl,yelp,bellow,whistl];
$a[6]=n;
// loop through nouns with index $i
foreach($n as$i=>$p) echo
    // print first part:
        // not first noun: print second line to previous step
        $i?"I hear a$a[$i] $p $v[$i]ing in my ear.\n\n":"",
        // uppercase the first letter of each word in the noun, print
        $q=ucwords($p),
        // print uppercase noun again and the rest of the line
        ", $q, what do you hear?\n",
        // for last noun print bridge, else print nothing
        $i>9?"I hear children...\n\n...":""
    // ... AND add line to second part
        &$z[]="$v[$i]ing like a$a[$i] $p"
;
// print second part and final line
echo join(",\n",$z),"...\nthat's what I hear.";

8

JavaScript中,545个 541 497字节

a="Polar Bear|Lion|Hippopotamus|Flamingo|Zebra|Boa Constrictor|Elephant|Leopard|Peacock|Walrus|Zookeeper".split`|`;s=btoa`º0:èj¼ìíÍùn·6ëk,áË3¶»¦¥ës²v«<ÛzYhÃ<!Ëe`.split`z`;a.map((m,n)=>{console.log(m+", "+m+", what do you hear?\n "+((n>9)?"I hear children...\n":"I hear a"+(n==5?"n ":" ")+a[n+1].toLowerCase()+" "+s[n+1]+"ing in my ear.\n"))});p="...";a.map((m,n)=>{if(n>9)return;p+=s[n]+"ing like a"+(n==6?"n ":" ")+m.toLowerCase()+(n>8?"...\nthat's what I hear.":",\n")});console.log(p)

相当有趣的挑战!

感谢Downgoat 使用,为我节省了27吨的字节atob,并为Titus 节省了4字节!

取消高尔夫:

a="Polar Bear|Lion|Hippopotamus|Flamingo|Zebra|Boa Constrictor|Elephant|Leopard|Peacock|Walrus|Zookeeper".split`|`;
s=btoa`º0:èj¼ìíÍùn·6ëk,áË3¶»¦¥ës²v«<ÛzYhÃ<!Ëe`.split`z`;
a.map((m,n)=>{
  console.log(m+", "+m+", what do you hear?\n "+((n==10)?"I hear children...\n":"I hear a" + (n==5?"n ":" ") + a[n+1].toLowerCase() + " " + s[n+1] + "ing in my ear.\n"))
});
p="...";a.map((m,n)=>{
  if(n==10) return;
  p+=s[n] + "ing like a" + (n==6?"n ":" ") + m.toLowerCase() + (n==9?"...\nthat's what I hear.":",\n")
});
console.log(p);

a.map((n,m)=>n>9||(p+=s[n]...))
尼尔

1
@ OldBunny2800为了打高尔夫球,您可以设置全局变量。(不过,请不要在递归函数中执行此操作!)
Neil

1
您的代码不起作用,我假设堆栈在您的base64代码中交换了一些字母。拆分后,我得到[“ ujA66Gq87O3N + W63NutrLOHLM7a7pqXrc7J2q”,“”,“ bellow”,“ why2U =”]。
lolesque's

1
首先定义c=console.log;,然后使用c()保存6个字节。或者只是将字符串保存在变量中,然后使用alert()打印一次。
lolesque '16

1
@lolesque The c=console.log不起作用,因为log必须具有thisof,console否则会产生错误。(除非不是在某些浏览器/环境中,否则?)将其全部放入字符串中的想法听起来不错,这是
apsillers

7

///523512字节

/_/\/\///:/ar_!/, what do you he:?
I he: _$/!a_#/ing in my e:.

_%/ing like a_&/, _*/,
_0/Pol: Be:_1/ion_2/ippopotamus_3/lamingo_4/ebra_5/oa _S/onstrictor_6/lephant_7/eopard_8/eacock_9/alrus_Q/ookeeper_R/trumpet/0&0$ l1 ro:#L1&L1$ h2 snort#H2&H2$ f3 flut#F3&F3$ z4 bray#Z4&Z4$ b5cS hiss#B5CS&B5CS$n e6 R#E6&E6$ l7 snarl#L7&L7$ p8 yelp#P8&P8$ w9 bellow#W9&W9$ zQ whistl#ZQ&ZQ!children...

...growl% pol: be:*ro:% l1*snort% h2*flut% f3*bray% z4*hiss% b5cS*R%n e6*snarl% l7*yelp% p8*bellow% w9...
that's what I he:.

在线尝试!

比实际消息(1120)少608个字节,这少于消息的一半(560)!


6

Dyalog APL,387 字节

⎕UCS¯2(219⌶)¯128+⎕UCS'øZýÓÁî°üÁ~`Û¯ß(4Õ+ÖjáïvXô ¶Dhkë ©4¹³`LxÖ4¢gͼ4ªBáDÚN¼ùV ÂÅF]@¸hÆà bKÚ¸_oQãÔÝ ¸$8§ú_ÔÈxÖ ~6*ãªd ¿ð>¥ÐB8þôÍxwÄ6Å
ô¡é¢wBlDµ¯ë
Ãbqè+ý+ÜÈýgu!gWËØ#0="«ë"HDÝÀV·¦/ÕMÆí}h<àì7å ÝÒëëñX³ýM <dÄ<ëmµñqP]¥c¬l¾¤ìÚã/$ÝÕqÙÞîCLéMÝÏ}OÔϤr$¤è©~ì2[¢{·YWÒ俦à¶)YØ©;s÷¬¥+ó&¿ÉâgËr
,Ýî©)¥Ö­2è´ÝÅd{úK5­·Õ}ýÚ'

为了简化阅读:⎕UCS¯2(219⌶)¯128+⎕UCS'...'

⎕UCS '... '将字符串转换为Unicode代码点

¯128+ 减去128得到-128≤n≤127

¯2(219⌶) 解压缩

⎕UCS 将Unicode代码点转换为字符串

源字符串是以下zlib编码的Unicode代码点的365个字节:

F8 5A FD D3 C1 EE 04 B0 8C FC C1 7E 60 DB AF 91 DF 28 34 D5 2B D6 6A E1 EF 15 76 96 58 F4 09 B6 44 11 93 04 68 6B EB A0 A9 94 34 B9 10 98 0F B3 60 4C 78 0C D6 91 1C 34 A2 89 67 CD BC 34 AA 42 95 E1 44 9E DA 4E BC 0B 0F F9 87 85 56 20 83 C2 C5 46 5D 40 B8 68 C6 E0 20 92 62 13 81 89 4B DA B8 5F 9A 6F 51 E3 D4 DD 9F A0 B8 24 38 A7 FA 5F D4 C8 78 7F D6 A0 7E 36 2A E3 AA 64 20 BF F0 3E A5 D0 42 9A 95 38 FE F4 CD 8A 78 99 77 C4 17 89 11 10 36 82 C5 0D 8A 9A F4 A1 12 E9 A2 92 77 9D 42 1E 6C 44 B5 AF EB 0D 04 C3 62 71 87 9C E8 2B FD 2B DC 04 C8 FD 67 75 21 67 57 04 CB D8 23 12 30 9A 3D 22 AB EB 22 48 9E 44 DD C0 56 B7 87 85 A6 2F D5 03 4D 9D C6 ED 7D 1E 68 3C E0 EC 37 9C 94 08 86 E5 09 DD D2 EB EB F1 58 B3 FD 4D 20 04 3C 17 64 C4 3C EB 6D B5 F1 11 89 71 50 5D A5 63 AC 6C 9A BE A4 EC DA E3 2F 24 DD D5 71 D9 DE EE 04 43 4C E9 4D DD CF 7D 4F 13 D4 CF 13 A4 72 24 A4 E8 1A 9E A9 7E EC 1F 32 5B 19 10 A2 7B B7 01 59 57 D2 E4 BF A6 E0 B6 29 94 59 D8 A9 3B 73 1D 94 9B F7 AC A5 2B F3 26 BF C9 E2 67 7F CB 72 0A 2C DD EE A9 29 A5 D6 AD 92 32 E8 B4 DD C5 64 7B FA 8A 4B 35 AD B7 D5 7D 82 0F FD 1 DA

6

Python 3中,497个 484 480字节

P=print
N=lambda m:"an"[:("E"==m[0])+1]+" "+m.lower()
h="I hear "
A=[a.split("|")for a in"Polar Bear|growl:Lion|roar:Hippopotamus|snort:Flamingo|flut:Zebra|bray:Boa Constrictor|hiss:Elephant|trumpet:Leopard|snarl:Peacock|yelp:Walrus|bellow:Zookeeper|whistl".split(":")]
for i in range(11):
 a,b=A[i]
 if i:P(h+N(a)+" "+b+"ing in my ear.\n")
 a+=", ";P(a+a+"what do you hear?")
P(h+"children...\n\n..."+",\n".join([z+"ing like "+N(y)for y,z in A[:-1]])+"...\nthat's what I hear.")

(保存的13个字节的信用应该交给GáborFekete,而Taylor Lopez则保存了另外4个字节。)

因此,您可能会猜到我将使用Python之类的语言作为教学工具。这是我开始打高尔夫球之前的程序。

def startsWithVowel(name):
    firstLetter = name[0]
    return firstLetter in ["A", "E", "I", "O", "U"]

def aOrAn(name):
    return "an" if startsWithVowel(name) else "a"

animals = [
#    NAME (0),     SOUND (1)
    ("Polar Bear", "growling"),
    ("Lion", "roaring"),
    ("Hippopotamus", "snorting"),
    ("Flamingo", "fluting"),
    ("Zebra", "braying"),
    ("Boa Constrictor", "hissing"),
    ("Elephant", "trumpeting"),
    ("Leopard", "snarling"),
    ("Peacock", "yelping"),
    ("Walrus", "bellowing"),
    ("Robot Dragon", "screeching"),
    ("Owl", "hooting"),
    ("Monkey", "laughing"),    
    ("Zookeeper", "whistling")]

NAME = 0
SOUND = 1
for animalNumber in range(len(animals)):
    thisAnimal = animals[animalNumber]
    if animalNumber + 1 < len(animals):    
        nextAnimal = animals[animalNumber + 1] 
    else: 
        nextAnimal = None

    print("{n}, {n}, what do you hear?".format(n=thisAnimal[NAME]))

    if nextAnimal != None:
        print("I hear {a} {n} {s} in my ear.".format(n=nextAnimal[NAME].lower(),
                                                     s=nextAnimal[SOUND],
                                                     a=aOrAn(nextAnimal[NAME])))
    else:
        print("I hear children...")

        children = []        
        for animal in animals[:-1]:
            children.append("{s} like {a} {n}".format(n=animal[NAME].lower(), 
                                                      s=animal[SOUND],
                                                      a=aOrAn(animal[NAME])))
        print("...{}...".format(",\n".join(children)))
        print("that's what I hear.")

2
您可以使用解压缩语法来保存一些字节:更改a=A[i]a,b=A[i],然后可以编写aand b代替a[0]and a[1]。这将节省12个字节。
加博尔·费克特(GáborFekete)

更改N=lambda m:("a","an")["E"==m[0]]+" "+m.lower()N=lambda m:"an"[:("E"==m[0])+1]n=a[0]+", ";P(n+n+"what do you hear?")a+=", ";P(a+a+"what do you hear?")会节省2多个字节。
加博尔·费克特(GáborFekete)

一般高尔夫问题。在这里len(A),由于挑战的目的只是生成静态输出(而不处理动态输入),因此使用硬编码11代替保存字节是否可以接受?
泰勒·洛佩兹

1
@TaylorLopez当然,通常这是一种不好的做法,但是对于高尔夫球场而言,其目标是最小化代码,我认为没有理由不这样做。
aaaantoine

5

Pyth - 427个 384字节

=G"1, 1, what do you hear?\nI hear a 2 in my ear.\n"=Hc"growling/Polar Bear/roaring/Lion/snorting/Hippopotamus/fluting/Flamingo/braying/Zebra/hissing/Boa Constrictor/trumpeting/Elephant/snarling/Leopard/yelping/Peacock/bellowing/Walrus/whistling/Zookeeper"\/FNT
::G\1@H+yN1\2++r@H+yN3Zd@H+yN2)p+::G"I(.|\n)*"k\1@H20"I hear children...\n\n..."FN9
::"1 like a 2,"\1@HyN\2r@H+yN1Z)p"bellowing like a walrus...\nthat's what I hear.

J"%s, %s, what do you hear?"K"I hear a%sing in my ear.\n"=Hc"Polar Bear/roar/Lion/snort/Hippopotamus/flut/Flamingo/bray/Zebra/hiss/Boa Constrictor/trumpet/Elephant/snarl/Leopard/yelp/Peacock/bellow/Walrus/whistl/Zookeeper/growl"\/FN11
%J*2[@HyN)?qNT"I hear children...\n"%Kjd[?qN5\nkr@H+yN2Z@HhyN))p"..."FN9
+%=J"%sing like a%s"[@HtyN+?qN5\nk+dr@HyNZ)?qN8"..."\,)
"that's what I hear"

在这里尝试- 永久链接


你能打高尔夫球ing吗?看到PHP击败Pyth,我发疯了。
泰特斯(Titus),2016年

1
代码输出a elephant
Antti29年

所有问题(包括PHP击败Pyth的事实)都已解决!
KoreanwGlasses

3

C#,575 525 520 526 507 504 497 494 493字节

()=>{var t="growl,Polar Bear,roar,Lion,snort,Hippopotamus,flut,Flamingo,bray,Zebra,hiss,Boa Constrictor,trumpet,Elephant,snarl,Leopard,yelp,Peacock,bellow,Walrus,whistl,Zookeeper".Split(',');string r="",s="",z="a ",n="an ";for(int i=0,j=0;i<21;r+=t[++i]+$", {t[i]}, what do you hear?\nI hear "+(i>19?@"children...

...":(i==11?n:z)+t[++i+1].ToLower()+$" {t[i]}ing in my ear.\n\n"))s+=j<20?t[j]+"ing like "+(j==12?n:z)+t[++j].ToLower()+(++j<20?@",
":@"...
"):"that's what I hear.";return r+s;};

C#lambda,其中输出为string

说明

  1. 初始化t为具有格式的字符串,noise,animal,noise,animal,...并用分隔,
  2. 声明rsr是歌曲的第一部分,s最后一部分。zn在这里处理Elephant
  3. 步行t和建造,rs在同一循环中。奇怪的三元+内插的东西。
  4. 返回歌曲的第一部分,然后返回最后一部分。

()=>{
    var t="growl,Polar Bear,roar,Lion,snort,Hippopotamus,flut,Flamingo,bray,Zebra,hiss,Boa Constrictor,trumpet,Elephant,snarl,Leopard,yelp,Peacock,bellow,Walrus,whistl,Zookeeper".Split(',');
    string r="",s="",z="a ",n="an ";
    for(int i=0,j=0;i<21;r+=t[++i]+$", {t[i]}, what do you hear?\nI hear "+(i>19?@"children...

...":(i==11?n:z)+t[++i+1].ToLower()+$" {t[i]}ing in my ear.\n\n"))
        s+=j<20?t[j]+"ing like "+(j==12?n:z)+t[++j].ToLower()+(++j<20?@",
":@"...
"):"that's what I hear.";
    return r+s;
};

在线尝试!


3

Python 2-454字节

a="polar bear,lion,hippopotamus,flamingo,zebra,boa constrictor,elephant,leopard,peacock,walrus,zookeeper,whistl,bellow,yelp,snarl,trumpet,hiss,bray,flut,snort,roar,growl".split(',')
e='...\n\n'
r=range
print'\n'.join([(a[i].title()+', ')*2+"what do you hear?\nI hear "+((i<10)*("a%s %sing in my ear.\n"%("n "[i!=5:]+a[i+1],a[~-~i]))or"children%s..."%e+",\n".join(a[~j]+"ing like a"+"n "[j!=6:]+a[j]for j in r(10))+e+"that's what I hear.")for i in r(11)])

3

[R 518 509 482 477 474个 465 452 456字节

链接到R-Fiddle尝试代码

a=c("Polar Bear","Lion","Hippopotamus","Flamingo","Zebra","Boa Constrictor","Elephant","Leopard","Peacock","Walrus","Zookeeper")
b=c("...growl","roar","snort","flut","bray","hiss","trumpet","snarl","yelp","bellow","whistl")
d=c(rep("a ",6),"an ")
f=tolower(a)
cat(c(paste0(a,", ",a,", what do you hear?\nI hear ",c(paste0(d,f," ",b,"ing in my ear.")[-1],"children..."),"\n\n"),paste0(b,"ing like ",d,f,c(rep(",\n",9),"...\nthat's what I hear."))[-11]))
  • 感动children了载体a,并在摆脱了相应条目的d矢量保存8个字节
  • 通过整理和删除下标来进行更多的整理和删除,从而children进一步节省了27个字节。
  • 全局化tolower功能,保存5个字节
  • 替换writeLinescat并重新格式化(\n在地方添加),节省了3个字节
  • 意识到如果d只有7个元素而不是全部11个元素,R会再次循环回到起点。当11 <14时,这对我们有效。保存了9个字节。
  • 全球化下标,节省了13个字节。
  • cat功能分开了" ",改回了writeLines。增加了4个字节(cat可以通过添加,sep=""到函数来固定 )

欢迎来到PPCG!这是一个不错的第一答案。有没有办法在线尝试您的代码?
aloisdg说恢复莫妮卡

1
尝试使用此r-fiddle.org/#/fiddle?id=5pqkHPyL&version=1, 尽管我必须添加一个字符(反斜杠)才能正确读取“ that's”中的撇号
Robert Maidstone

谢谢。在R-Fiddle中,我看到您' 'aafter的每个项目都有一个before "Polar Bear"。正常吗
aloisdg说恢复莫妮卡

我不确定您的意思。您是说诗歌第一部分中的空格吗?如在“狮子”之前的空间中?“北极熊,北极熊……\ n我听到一个……\ n狮子,狮子..... \ n我听到一个……”我以前没有注意到这一点。它来自使用空格分隔的cat函数。可以通过在函数中添加sep =“”来解决此问题,但是最好使用writeLines。(我将代码改回了writeLines)
罗伯特·梅德斯通

3

C#,592572字节

using System;class p{static void Main(){string[] s={"","","Polar Bear","Lion","Hippopotamus","Flamingo","Zebra","Boa Contrictor","Elephant","Leopard","Peacock","Walrus","Zookeeper","growl","roar","snort","flut","bray","hiss","trumpet","snarl","yelp","bellow","whistl"};for(int i=2;i<13;i++){s[0]+=(s[i]+", "+s[i]+", what do you hear?\nI hear "+(i<12?"a"+(i==7?"n ":" ")+s[i+1].ToLower()+" "+s[i+12]+"ing in my ear.\n\n":"children...\n\n..."));if(i<12)s[1]+=s[i+11]+"ing like a"+(i==8?"n ":" ")+s[i].ToLower()+(i<11?",\n":"...\nthat's what I hear.\n");}Console.Write(s[0]+s[1]);}}

和我的同事一起度过了如此愉快的时光!谢谢你的主意。

格式:

using System;
class p {
    static void Main() {
        string[] s = { "", "", "Polar Bear", "Lion", "Hippopotamus", "Flamingo", "Zebra", "Boa Contrictor", "Elephant", "Leopard", "Peacock", "Walrus", "Zookeeper", "growl", "roar", "snort", "flut", "bray", "hiss", "trumpet", "snarl", "yelp", "bellow", "whistl" };
        for (int i = 2; i < 13; i++) {
            s[0] += (s[i] + ", " + s[i] + ", what do you hear?\nI hear " + (i < 12 ? "a" + (i == 7 ? "n " : " ") + s[i + 1].ToLower() + " " + s[i + 12] + "ing in my ear.\n\n" : "children...\n\n..."));
            if (i < 12) s[1] += s[i + 11] + "ing like a" + (i == 8 ? "n " : " ") + s[i].ToLower() + (i < 11 ? ",\n" : "...\nthat's what I hear.\n");
        }
        Console.Write(s[0] + s[1]);
        Console.ReadKey();
    }
}

编辑:谢谢您的无数绝妙提示,它们提供了很多帮助,我将尽力使其他解决方案也能正常工作。


1
删除所有不必要的空格,可以使用new[]o代替。new string[]您也可以将其变成匿名函数,例如@aloisdg的答案
TheLethalCoder '16

欢迎来到PPCG!这个答案好主意!您可以在C#页面中的代码高尔夫球技巧中找到一些改进的灵感。继续!
aloisdg说恢复莫妮卡

1
您掉入了a elephant陷阱(我也是)。是an elephant;)
aloisdg说莫妮卡

取出动词数组中的“ ing”并将其移至输出以节省更多。
GuitarPicker

3

分批,650个 647字节

@echo off
set e=echo(
set s=shift
call:d a "polar bear" ...growl "Polar Bear" a lion roar Lion a hippopotamus snort Hippopotamus a flamingo flut Flamingo a zebra bray Zebra a "boa constrictor" hiss "Boa Constrictor" an elephant trumpet Elephant a leopard snarl Leopard a peacock yelp Peacock a walrus bellow
exit/b
:d
call:v %* Walrus a zookeeper whistl Zookeeper
%e%I hear children...
%e%
:l
%e%%3ing like %1 %~2,
%s%
%s%
%s%
%s%
if not "%5"=="" goto l
%e%%3ing like a %2...
%e%that's what I hear.
exit/b
:v
%e%%~4, %~4, what do you hear?
if "%5"=="" exit/b
%e%I hear %5 %~6 %7ing in my ear.
%e%
%s%
%s%
%s%
%s%
goto v

因为%*Batch最接近数组。如果我可以使用Polar bearBoa constrictor那么我可以节省10%...但是我仍然占原始文本的50%...

编辑:通过设置e=echo(a (而不是空格节省了3个字节,这使我可以用它来回显空白行,尽管我随后必须@echo off完整写出。


2

Bash + zcat,402字节

只是开个玩笑:
一个2行的bash脚本,它会自己尾巴并将第二行传送到zcat。
第二行是压缩文本。

如何准备:文件中的所有文本a。然后

echo "tail -n+2 \$0|zcat;exit" > go.sh
gzip a ;
cat a.gz >> go.sh
chomod u+x go.sh

这样执行时创建的文件将返回起始文本402字节。


您可以发布实际的代码吗?402个字节是不是多(即你可以将它张贴。)
埃里克Outgolfer

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ该代码后仅tail -n+2 \$0|zcat;exit跟一个换行符和一个379字节的二进制字符串(建议的文本的压缩版本)。您知道投票否决的原因吗?(这是bash自我安装脚本的半标准过程,脚本内部有一个存档,恕我直言,与在c ++中使用库没什么不同)。
哈斯塔

看来您使用的是coreutils(zcat),因此标头必须为(Bash + coreutils,402 bytes)而不是(Bash(with zcat)402)。而且,您可以编辑答案(只需<!>在帖子末尾附加)吗?我要删除
下注

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ zcat不是来自coreutils,它带有gzip。在许多系统中,都有一个调用该脚本的脚本gzip -cd "$@"... BTW我用Bash + zcat修改了402字节。
哈斯图

很好,删除了downvote。赞成票是我的。
暴民埃里克(Erik the Outgolfer)'16年

2

F#,591 589 579 576 573 567 552个字节

这是我第一次尝试使用我喜欢的语言编写代码高尔夫。我敢打赌,有一些方法可以将其缩短。在FSI中运行,因此不需要.NET样板。

编辑:通过删除剃光了10个字节\r。它仍然可以在VSCode中很好地渲染,所以去吧。剪切另外三个具有函数组成的字节,剪切另外三个具有lambda的字节,然后剪切六个for循环而不是Seq.iter的字节(摆脱lambda)。最后,通过重新排列减少了15个字节a(s:string)

let A="Polar Bear,Lion,Hippopotamus,Flamingo,Zebra,Boa Constrictor,Elephant,Leopard,Peacock,Walrus,Zookeeper".Split(',')
let S="growl,roar,snort,flut,bray,hiss,trumpet,snarl,yelp,bellow,whistl".Split(',')
let a(s:string)=(if s.[0]='E'then"an "else"a ")+s.ToLower()
let f i=sprintf"%s, %s, what do you hear?\nI hear "A.[i]A.[i]
for i in 0..9 do printfn"%s%s %sing in my ear.\n"(f i)(a A.[i+1])S.[i+1]
printf"%schildren...\n\n..."(f 10)
let t i=sprintf"%sing like %s"S.[i](a A.[i])
Seq.iter(t>>printfn"%s,")[0..8]
printfn"%s...\nthat's what I hear."(t 9)

3
欢迎使用PPCG,好答案!

我知道#F是.net languange,但我认为出于这一挑战的目的,您可以用简单的“ \ n”代替“ \ r \ n”,因为概念相同并且可以节省字节
Vale

@Vale谢谢。保存了10个字节
asibahi,2016年

1
不会影响计数,但是您拼写的“河马”错误。
GentlePurpleRain '16

@GentlePurpleRain嘿。我以为我在寻找所有的拼写。我会修好它。
asibahi

1

Emacs Lisp,576(621)字节

(let*((a'("polar bear""lion""hippopotamus""flamingo""zebra""boa constrictor""elephant""leopard""peacock""walrus""zookeeper"))(l(length a))(m(- l 1))(s'("growl""roar""snort""flut""bray""hiss""trumpet""snarl""yelp""bellow""whistl")))(dotimes(i l)(message"%s, what do you hear?"(let((a(capitalize(nth i a))))(concat a", "a)))(if(not(eq i m))(message "I hear a %s %sing in my ear.\n"(nth(1+ i)a)(nth(1+ i)s))(message"I hear children...\n")(dotimes(j m)(message(concat(when(eq j 0)"...")"%sing like a %s"(if(eq(1+ j)m)"..."","))(nth j s)(nth j a)))(message"That's what I hear."))))

修复“大象”时,需要花费更长的时间:

(let*((a'("polar bear""lion""hippopotamus""flamingo""zebra""boa constrictor""elephant""leopard""peacock""walrus""zookeeper"))(l(length a))(m(- l 1))(s'("growl""roar""snort""flut""bray""hiss""trumpet""snarl""yelp""bellow""whistl")))(dotimes(i l)(message"%s, what do you hear?"(let((a(capitalize(nth i a))))(concat a", "a)))(if(not(eq i m))(message "I hear a %s %sing in my ear.\n"(nth(1+ i)a)(nth(1+ i)s))(message"I hear children...\n")(dotimes(j m)(message(concat(when(eq j 0)"...")"%sing like a"(and(string-match-p"^[aeiou]"(nth j a))"n")" %s"(if(eq(1+ j)m)"..."","))(nth j s)(nth j a)))(message"That's what I hear."))))

取消高尔夫:

(let* ((animals '("polar bear"
                  "lion"
                  "hippopotamus"
                  "flamingo"
                  "zebra"
                  "boa constrictor"
                  "elephant"
                  "leopard"
                  "peacock"
                  "walrus"
                  "zookeeper"))
       (l (length animals))
       (m(- l 1))
       (sounds '("growl"
                 "roar"
                 "snort"
                 "flut"
                 "bray"
                 "hiss"
                 "trumpet"
                 "snarl"
                 "yelp"
                 "bellow"
                 "whistl")))
  (dotimes (i l)
    (message "%s, what do you hear?"
             (let ((animal (capitalize (nth i animals))))
               (concat animal ", " animal)))
    (if (not (eq i m))
        (message "I hear a %s %sing in my ear.\n"
                 (nth (1+ i) animals)
                 (nth (1+ i) sounds))
      (message "I hear children...\n")
      (dotimes (j m)
        (message
         (concat
          (when (eq j 0) "...")
          "%sing like a"
          (and (string-match-p"^[aeiou]" (nth j animals)) "n")
          " %s"
          (if (eq (1+ j) m) "..." ","))
         (nth j sounds )
         (nth j animals)))
      (message"That's what I hear."))))

您的正则表达式可以从缩短^[aeiou]^e,不是吗?
owacoder '16

对于大象来说,是的,但是如果添加其他动物,则需要进行更改。已经有了600个字节,那么6个字节并不会带来太大的伤害。
尤玛勋爵

1

C,596字节

调用时f()不带任何参数。这不是世界上最好的高尔夫,我可能会进一步缩小。

f(n){char*t,a[99][99]={"Polar Bear","Lion","Hippopotamus","Flamingo","Zebra","Boa Constrictor","Elephant","Leopard","Peacock","Walrus","Zookeeper","growl","roar","snort","flut","bray","hiss","trumpet","snarl","yelp","bellow","whistl"};for(n=0;n<11;){strcpy(t=a[n+30],n^6?" ":"n ");strcat(t,a[n++]);for(;*t=tolower(*t);++t);}for(n=0;printf("%s, %s, what do you hear?\n",a[n],a[n]),n<10;++n)printf("I hear a%s %sing in my ear.\n\n",a[n+31],a[12+n]);puts("I hear children...\n");for(n=11;n<21;++n)printf("%s%sing like a%s%s\n",n^11?"":"...",a[n],a[n+19],n^20?",":"...");puts("that's what I hear.");}

在ideone上尝试。


您可以添加一个链接到ideone吗?
aloisdg说恢复莫妮卡

2
添加了ideone链接。
owacoder '16

a[99][99]-> **a(-6)。您不能f()在此处不带参数的情况下进行调用,需要使用n。替换f(n){f{n;。另外-1。另外,f()现在可以调用。
暴民埃里克(Erik the Outgolfer)'16

@owacoder怎么来?**a表示字符数组(或字符指针指针)的数组,对我而言,它一直有效(gcc 4.8)。您使用什么编译器?如果您不使用gcc,则必须指定它。如果使用gcc,则为默认设置。
暴民埃里克(Erik the Outgolfer)'16年

@owacoder不!它使数组适合长度为98的98个字符串!而且,您绝对不应该将ideone链接称为“在线尝试!” 要打高尔夫球,请替换a[99][99]a[<how many elements do I modify?>+1][<what is the maximum length of any string in here?>+1]
暴民埃里克

1

Python 3中,442个 441字节

h=t=''
for n,s in zip('polar bear,lion,hippopotamus,flamingo,zebra,boa constrictor,elephant,leopard,peacock,walrus,zookeeper'.split(','),'...growl roar snort flut bray hiss trumpet snarl yelp bellow whistl'.split()):u='n'[:'el'in n],n,s,n.title();h+="I hear a{0} {1} {2}ing in my ear.\n\n{3}, {3} what do you hear?\n".format(*u);t+="{2}ing like a{0} {1},\n".format(*u)
print((h+'I hear children...\n\n'+t)[44:-30]+"...\nthat's what I hear.")

松散:

animals = 'polar bear,lion,hippopotamus,flamingo,zebra,boa constrictor,elephant,leopard,peacock,walrus,zookeeper'.split(',')

sounds = '...growl roar snort flut bray hiss trumpet snarl yelp bellow whistl'.split()

h=t=''
for n,s in zip(animals,sounds):
    u='n'[:'el'in n], n, s, n.title()

    h+="I hear a{0} {1} {2}ing in my ear.\n\n{3}, {3} what do you hear?\n".format(*u)

    t+="{2}ing like a{0} {1},\n".format(*u)

print((h+'I hear children...\n\n'+t)[44:-30]+"...\nthat's what I hear.")

基本上,只是一个循环,该循环建立了代表故事的两个部分的两个字符串。该'n'[:'el'in n]手柄“的”大象的。该[44:-30]关印章领先“我听到一个北极熊在我耳边咆哮”和结尾的“吹口哨就像一个动物园管理员”。


1

QB64(QuickBASIC),581字节

将代码放入解释器时,代码会扩展一点,但是此源文件可以按原样加载。如果要在滚动之前看到整个输出,则需要width 80,46在开头添加一条语句。

READ a$,v$
FOR x=0 TO 10
?a$+", "+a$+", what do you hear?"
IF x>9THEN
?"I hear children...":?:?"...";
RESTORE
FOR y=0 TO 9
READ a$,v$
?v$+"ing like ";
CALL j(a$)
IF y<9THEN?", "ELSE?"...":?"that's what I hear."
NEXT
END
ELSE
READ a$,v$
?"I hear ";
CALL j(a$)
?" "+v$+"ing in my ear."
?
END IF
NEXT
DATA"Polar Bear","growl","Lion","roar","Hippopotamus","snort","Flamingo","flut","Zebra","bray","Boa Constrictor","hiss","Elephant","trumpet","Leopard","snarl","Peacock","yelp","Walrus","bellow","Zookeper","whistl"
SUB j (A$)
?"a";
IF LEFT$(A$,1)="E"THEN?"n";
?" "+LCASE$(A$);
END SUB

1
我知道这有点晚了,但是:我认为您可以使用GOSUB/ RETURN代替CALL/ SUB/ 来节省一些字节END SUB。另外,您可以DATA为所有没有空格的条目添加引号。最后,由于只有一种以元音开头的动物,A$="Elephant"因此实际上比短1个字节LEFT$(A$,1)="E"。祝您使用RESTORE愉快,顺便说一句!
DLosc

1

LUA 535字节

p=print a="growl.roar.snort.flut.bray.hiss.trumpet.snarl.yelp.bellow.whist"b="polar bear.lion.hippopotamus.flamingo.zebra.boa constrictor.elephant.leopard.peacock.walrus.zookeeper."z=a.gmatch(a,"%P+")x=b.gmatch(b,"%P+")w=''for j=1,11 do i=x()v=z()e=j==7 and'n 'or' 'p(j==1 and''or'I hear a'..e..i..' '..v..'ing in my ear.\n')w=w..(j>10 and''or v..'ing like a'..e..i..(j>9 and'...'or',')..'\n')y=i:gsub("^%l",a.upper):gsub("%s%l",a.upper)p(y..', '..y..' what do you hear?')end p('I hear children...\n')p('...'..w.."that's what I hear.")

松散:

sound_list = "growl.roar.snort.flut.bray.hiss.trumpet.snarl.yelp.bellow.whist"
animal_list = "polar bear.lion.hippopotamus.flamingo.zebra.boa constrictor.elephant.leopard.peacock.walrus.zookeeper."

sound_it = string.gmatch(sound_list, "%P+")
animal_it = string.gmatch(animal_list, "%P+")
part_2 = ''

for i = 1, 11  do
    animal = animal_it()
    sound = sound_it()
    aORan = i == 7 and 'n ' or ' '

    print(i == 1 and '' or 'I hear a'..aORan..animal..' '..sound..'ing in my ear.\n')

    part_2 = part_2..(i > 10 and '' or sound..'ling like a'..aORan..animal..(i > 9 and '...' or ',')..'\n')

    big_animal = animal:gsub("^%l", string.upper):gsub("%s%l", string.upper)
    print(big_animal..', '..big_animal..' what do you hear?')
end

print('I hear children...\n')
print('...'..part_2.."that's what I hear.")

在线尝试


1

PHP,366字节

懒惰的答案,但是为什么在gzip可用时编写自定义的解压缩器?

PHP代码的十六进制转储(可以用xxd -r保存)

0000000: 3c3f 3d67 7a69 6e66 6c61 7465 2827 7d53  <?=gzinflate('}S
0000010: c16a 8430 10bd e72b e6d6 cbe0 2f14 b6b4  .j.0...+..../...
0000020: b4d0 c3de 0abd 8d6e baca c64c 1823 62bf  .......n...L.#b.
0000030: be51 3354 1472 58f3 76de f864 e6bd 5cd9  .Q3T.rX.v..d..\.
0000040: 91c0 c592 20ec f1d4 5284 1bc3 cc23 b4a9  .... ...R....#..
0000050: f26c 3ed6 1308 5cc7 1e84 493a 7f87 ce43  .l>...\...I:...C
0000060: 3f43 222a 633e 1381 b03d 0bef b75d 081c  ?C"*c>...=...]..
0000070: 3852 3f0e 3078 9678 147a df75 20ec ff15  8R?.0x.x.z.u ...
0000080: 857f 1cf5 498a 1318 4f9a 6f99 4450 54d4  ....I...O.o.DPT.
0000090: fab5 b510 a4df 7c14 fa5e 1884 7c14 246a  ......|..^..|.$j
00000a0: 2668 d80f 51ba 26b2 a4b9 87e1 2876 6182  &h..Q.&.....(va.
00000b0: 97ff 1e84 63a1 f001 0fd6 d9d0 928f 1065  ....c..........e
00000c0: ec83 3dcd fc9a 7904 4565 632d 0792 5bf2  ..=...y.Eec-..[.
00000d0: 84c4 9dcc dd48 0405 25a5 60a9 e1e6 01b3  .....H..%.`.....
00000e0: 75e1 2874 dd38 0405 25a1 899c a494 d4d6  u.(t.8..%.......
00000f0: 399e 8e4a 5f2b 89a0 67c9 4ee6 87b5 c10a  9..J_+..g.N.....
0000100: 4cc9 86e8 4ea6 2a8f a0b0 b4fb a6ed dc4d  L...N.*........M
0000110: acaf aaf4 6e7a dc85 a755 d375 0fbb ccbf  ....nz...U.u....
0000120: dea4 3ab5 a211 2651 2adf 1e34 1a7b adb6  ..:...&Q*..4.{..
0000130: fb8c 1bcd 6f26 35d7 6896 3c2a a121 45a3  ....o&5.h.<*.!E.
0000140: c1ca d543 eed0 683a b465 971c 34d9 6ca5  ...C..h:.e..4.l.
0000150: 3404 68d4 3b1d 29bb 65b2 17ca a847 cb2a  4.h.;.).e....G.*
0000160: 624b f169 d8d6 b66d aa32 7f27 293b       bK.i...m.2.');

我使用了zopfli的一些迭代将源文本转换为349字节的压缩数据,避免了使用''字符,否则将不得不使用反斜杠对其进行转义。


1

Japt308 303 299 295 288 280 294 289 286字节

包含一堆无法打印的内容;请点击以下链接查看它们。

`Pol BÁLiyHippopotam«yFlaÚÁoyZeßBoa CÆqÎtyE¤pÊCyLeopÂýPea¬ckyWalr«yZookeep`qy
`gwÓ2ÂüÍdfl©dßdÊdtruÛFdsnÓ)lpdÞ)owdØtl`qd
[¡[X',SX`, Ø º y Ê#?
I Ê# `Y<A?[Y¶5?``:'aSUg°Y v SVgY `g  my e.`R]¬:[`å,Á`Q='.³R²QU¯J £[Vv `g ¦ke `Y¶6?``:'aSXv]¬Ãq`,
` QR`È  's Ø I Ê#.`]¬]¬Ã·]¬

测试一下

+14个字节“因为我想把海象弄成一团”;下周将需要再次通过它,看看我是否可以收回这些字节中的任何一个。


1

PowerShell的921 889 886 898个字节

它不是完美的,但是可以节省300字节:)

编辑: 谢谢乔纳森·弗雷奇,我保存了32个字节。并感谢Veskah纠正了输出。

filter Z($1,$2){while($1){$x,$1=$1
$y,$2=$2
[tuple]::Create($x,$y)}}
$0="ing"
$$="hear"
$1="I $$ a "
$3='in my ear.'
$a='Polar Bear'
$b='Lion'
$c='Hippopotamus'
$d='Flamingo'
$e='Zebra'
$f='Boa Constrictor'
$g='Elephant'
$h='Leopard'
$i='Peacock'
$j='Walrus'
$k='Zookeeper'
$v="children"
$2="roar$0"
$4="snort$0"
$5="flut$0"
$6="bray$0"
$7="hiss$0"
$8="trumpet$0"
$9="snarl$0"
$x="yelp$0"
$y="bellow$0"
$^="whistl$0"
$l="$1$b $2$3"
$m="$1$c $4$3"
$n="$1$d $5$3"
$o="$1$e $6$3"
$p="$1$f $7$3"
$q="I $$ an $g $8$3"
$r="$1$h $9$3"
$s="$1$i $x$3"
$t="$1$j $y$3"
$u="$1$k $^$3"
$z=' like a '
$w="I $$ $v..."
z $a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k $l,$m,$n,$o,$p,$q,$r,$s,$t,$u,$w|%{"$($_.item1),"*2+"what do you $$?";$_.item2;""}
"...growl$0$z$a,"
z $b,$c,$d,$e,$f $2,$4,$5,$6,$7|%{$_.item2+$z+$_.item1+","}
"$8 like an $g,"
z $h,$i $9,$x,$y|%{$_.item2+$z+$_.item1+","}
$y+$z+"Walrus..."
"that's what I $$."

不会使用标准的7位ASCII字符而不是ö更高的字节效率吗?
乔纳森·弗雷希

此外,我认为第一行中有一些多余的空格,以及长函数名称。
乔纳森·弗雷奇

谢谢你的提示:)!(我使用Ö只是因为教过我已经使用了所有可用的ASCII符号XD)
Add-TheNewOne

ö在某些代码页中,该字符仅使用单个字节表示,UTF-8要求此字符使用两个字节。
乔纳森·弗雷希

您的输出有点关。最后一个节重复了Polar-bear,将所有内容都消除了,您还包括了不需要的啸叫声。它还缺少省略号。它还写道:an elephant较早的节之间还有一个额外的换行符,可以通过删除来消除"n"
Veskah

0

clojure,526字节

(use 'clojure.string)(let[l lower-case
a(partition 2(split"Polar Bear,...growl,Lion,roar,Hippopotamus,snort,Flamingo,flut,Zebra,bray,Boa Constrictor,hiss,Elephant,trumpet,Leopard,snarl,Peacock,yelp,Walrus,bellow,Zookeeper,whistl,children,x"#","))b(map(fn[[w x][y e]][(str
w", "w", what do you hear?
I hear a "(l y)" "e"ing in my ear.

")(str x"ing like a "(l w))])a(rest a))r
replace](print(r(str(r(join""(map first b))#".*x.*""I hear children...")(join",
"(butlast(map last b)))"...
that's what I hear.")#"(?i)a(?= e)""an")))

这是我最初的尝试。一定喜欢格式以及它的可读性。希望来自的警告use 'clojure.string是可以接受的。

在线尝试。

格式化代码:

(use 'clojure.string)
(let [animal-sound (partition 2 (split "Polar Bear,...growl,Lion,roar,Hippopotamus,snort,Flamingo,flut,Zebra,bray,Boa Constrictor,hiss,Elephant,trumpet,Leopard,snarl,Peacock,yelp,Walrus,bellow,Zookeeper,whistl,children,x" #","))
      sentences (map (fn [[animal-1 sound-1] [animal-2 sound-2]]
                       [(str animal-1 ", " animal-1 ", what do you hear?\nI hear a "
                             (lower-case animal-2) " " sound-2 "ing in my ear.\n\n") (str sound-1 "ing like a " (lower-case animal-1))])
                     animal-sound (rest animal-sound))]
  (print (replace (str
                    (replace (join "" (map first sentences)) #".*x.*" "I hear children...")
                    (join ",\n" (butlast (map last sentences)))
                    "...\nthat's what I hear.") #"(?i)a(?= e)" "an")))

0

Java中,571 555 538个516或字节

String p(){String r="",z="...",b,x;String[]a="Polar Bear!Lion!Hippopotamus!Flamingo!Zebra!Boa Constrictor!Elephant!Leopard!Peacock!Walrus!Zookeeper!growling!roaring!snorting!fluting!braying!hissing!trumpeting!snarling!yelping!bellowing!whistling".split("!");for(int i=1;i<11;i++){b=i==6?"n ":" ";x=a[i-1];z+=a[i+10]+" like a"+b+x+(i==10?"...":"\n");r+=x+" , "+x+", what do you hear?\n I hear a"+b+a[i].toLowerCase()+" "+a[i+11]+" in my ear.\n\n";}r+=a[10]+" what do you hear?\n\tI hear children...\n"+z+"\nthat's what I hear.";return r;}

但是只需要516个字节即可在JShell中获得结果

String r="",z="...",b,x;String[]a="Polar Bear!Lion!Hippopotamus!Flamingo!Zebra!Boa Constrictor!Elephant!Leopard!Peacock!Walrus!Zookeeper!growling!roaring!snorting!fluting!braying!hissing!trumpeting!snarling!yelping!bellowing!whistling".split("!");for(int i=1;i<11;i++){b=i==6?"n ":" ";x=a[i-1];z+=a[i+10]+" like a"+b+x+(i==10?"...":"\n");r+=x+" , "+x+", what do you hear?\n I hear a"+b+a[i].toLowerCase()+" "+a[i+11]+" in my ear.\n\n";}r+=a[10]+" what do you hear?\n\tI hear children...\n"+z+"\nthat's what I hear."

它给出了关于空格,制表符,小写和不正确的输出anVS a.. 我固定的,golfed到507个字节
凯文Cruijssen

@KevinCruijssen您的代码缺少“我在耳边听到…………**”的“ ing”。此外,通过启动很可能golfable i0多达10变化b=a[i-1]b=a[i++]和消除目前它是增量。
奥利维尔·格雷戈尔

0

Swift2,519 字节

var a=["Polar Bear","Lion","Hippopotamus","Flamingo","Zebra","Boa Constrictor","Elephant","Leopard","Peacock","Warlus","Zookeeper"],b=["...growl","roar","snort","flut","bray","hiss","trumpet","snarl","yelp","bellow","whistl"].map{"\($0)ing"},c=a.enumerate().map{"\($0.0==6 ?"an":"a") \($0.1)".lowercaseString},i=0,j=0
while i<11{print("\(a[i]), \(a[i++]), what do you hear?\nI hear \(i>10 ?"children...":"\(c[i]) \(b[i]) in my ear")\n")}
while j<10{print("\(b[j]) like \(c[j++])\(j>9 ?"...\nthat's what I hear.":",")")}

0

哈斯克尔537 499个 497字节

import Data.Char
a&b=a++b
b=putStr
c=concat
d=init
e('E':s)="an e"&s
e s="a "&map toLower s
f="I hear "
l=zip(lines"Polar Bear\nLion\nHippopotamus\nFlamingo\nZebra\nBoa Constrictor\nElephant\nLeopard\nPeacock\nWalrus\nZookeeper")$words"growl roar snort flut bray hiss trumpet snarl yelp bellow whistl"
main=(b.drop 41.c)[f&e n&" "&v&"ing in my ear.\n\n"&n&", "&n&", what do you hear?\n"|(n,v)<-l]>>b(f&"children...\n\n..."&(d.d.c)[v&"ing like "&e n&",\n"|(n,v)<-d l]&"...\nthat's what I hear.\n")

使用解决方案没有可执行的约定,仅通过某些函数产生输出,这可以缩短为479字节。输出在g

import Data.Char
a&b=a++b
c=concat
d=init
e('E':s)="an e"&s
e s="a "&map toLower s
f="I hear "
l=zip(lines"Polar Bear\nLion\nHippopotamus\nFlamingo\nZebra\nBoa Constrictor\nElephant\nLeopard\nPeacock\nWalrus\nZookeeper")$words"growl roar snort flut bray hiss trumpet snarl yelp bellow whistl"
g=(drop 41.c)[f&e n&" "&v&"ing in my ear.\n\n"&n&", "&n&", what do you hear?\n"|(n,v)<-l]&f&"children...\n\n..."&(d.d.c)[v&"ing like "&e n&",\n"|(n,v)<-d l]&"...\nthat's what I hear.\n"

0

C(clang),552字节

(*P)()=printf;l(char*s){for(;*s;s++)putchar(*s>64?*s|32:*s);}char*a[]={"Zookeeper","Walrus","Peacock","Leopard","Elephant","Boa Constrictor","Zebra","Flamingo","Hippopotamus","Lion","Polar Bear"},*s[]={"whistl","bellow","yelp","snarl","trumpet","hiss","bray","flut","snort","roar","growl"};f(i){for(i=11;i--;)P("%s, %s, what do you hear?\nI hear%s ",a[i],a[i],i?i^5?" a":" an":""),i?l(a[i-1]):0,P(i?" %sing in my ear.\n\n":"children...\n\n...",s[i-1]);for(i=11;--i;)P("%sing like a%s ",s[i],i^4?"":"n"),l(a[i]),P(i-1?",\n":"...\nthat's what I hear.");}

在线尝试!



0

Perl 5、413字节

@t=('Polar Bear',growl,Lion,roar,Hippopotamus,snort,Flamingo,flut,Zebra,bray,'Boa Constrictor',hiss,Elephant,trumpet,Leopard,snarl,Peacock,yelp,Walrus,bellow,Zookeeper,whistl);say$_=$t[$i],", $_, what do you hear?
I hear ",/Zo/?"children...
":"a".n x(/C/).lc" $t[$i+=2] $t[$i+1]ing in my ear.
"while!/Zo/;pop@t;pop@t;print'...';say+(shift@t)."ing like a ".lc.(@t?",":'...')while$_=shift@t;say"that's what I hear."

在线尝试!


0

JavaScript,424字节

f=(x=0,a=btoa`>Z¯ ^j¼Ëó*i¢-jk¬ÌYZ)à£6^n¶³²
춸¶óW©©íÌ·¨¥ªÝÌ÷r$Íf¥®ë3f$yê^¯0`[s=`split`]`y`.join` `[s]`z`,g=z=>n[z]+`ing like `+h(17)+l(z)+(z>9?`...
that's what I hear.`:`,
`+g(++z)),h=m=>++x%m?`a `:`an `,l=y=>a[y].toLowerCase(n=btoa`º0:èj¼ìíÍùn·6ëk,áË3¶»¦¥ës²v«<ÛzYhÃ<!ËeÌ`[s]`z`))=>a[x]+`, ${a[x]}, what do you hear?
I hear `+(x>9?`children...

...`+g(0):h(6)+l(x)+` ${n[x]}ing in my ear.

`+f(x))

测试一下


0

Pyth,342个字节

L:b"a e""an e"y>sm.F"
I hear a {1} {0} in my ear.

{2}, {2}, what do you hear?"+dr3edJC,+R"ing"c"growl
roar
snort
flut
bray
hiss
trumpet
snarl
yelp
bellow
whistl"bc"polar bear
lion
hippopotamus
flamingo
zebra
boa constrictor
elephant
leopard
peacock
walrus
zookeeper"b42"I hear children...
"jPPysm+j" like a "d",
"PJc2*6\."that's what I hear.

在这里在线尝试。

解释如下。

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.