老麦克唐纳函数


16

用您选择的语言创建一个函数,该函数将输出以下内容:

Old MacDonald had a farm, E-I-E-I-O,
And on that farm he had a cow, E-I-E-I-O,
With a moo moo here and a moo moo there,
Here a moo, there a moo, everywhere a moo moo,
Old MacDonald had a farm, E-I-E-I-O!

例如,其中cowmoo是函数参数中的字符串,可以更改为pigoinksheepbaa

它应考虑大写字母,连字符,标点符号,空格和换行符。

旨在在代码中键入最少的Unicode字符。


1
不过这不是kolmogorov复杂性吗?
mniip 2014年

6
你说echoes the following。您的意思是该函数应该将其打印出来还是将其返回?
cjfaure

2
为了公平起见,我认为这应该在问题中有确切的标点符号,空格和回车符。但是您如何看待大写/小写?我认为单一情况可能会更容易,并且避免了使用base64或类似标准应用程序的负担。总而言之,唯一的大写字母是该行的开头,MacDonald一词,也位于EIEIO中,因此也可以根据每个问题进行精确编码。
级圣河

4
输出是可以接受的a oink还是应该做到an oink
ClickRick 2014年

4
@ rybo111:您是否知道伟大的唐纳德·克努斯(Donald Knuth)就这种事情写了一篇学术论文?它实际上是在真实的期刊上发表的(必须在4月版中发表)。此处提供的更多信息,包括论文PDF的链接:en.wikipedia.org/wiki/The_Complexity_of_Songs
Tom Chantler 2014年

Answers:


15

Javascript ES6-204

旨在在代码中键入最少的Unicode字符。

不是较短,但可能是最模糊的。

f=(a,b)=>{for(c=[b,a].concat('!,-ADEHIMOWacdefhilmnortvwy \n'.split(o='')),i=62;i;o+=c[e>>10]+c[e/32&31]+c[e&31])e='ⱞᄤ⒇瓤抣瘭㾭癍㚏᫶⦮函࿋Π疽䌐獲樘ྰ㞠戝晐}疽䌐࿈䌐眲Π疽㛏戝癐Π疽伲࿌⒋ფᲉѽ疽䦯䨝抽瘭䦹容㾷碶ᅣᲉᄤྦྷ㜕㞱㗽㾲妴㣗畍⺏'.charCodeAt(--i);alert(o)}

如果您的浏览器不支持ES6:

function f(a,b){for(c=[b,a].concat('!,-ADEHIMOWacdefhilmnortvwy \n'.split(o='')),i=62;i;o+=c[e>>10]+c[e/32&31]+c[e&31])e='ⱞᄤ⒇瓤抣瘭㾭癍㚏᫶⦮函࿋Π疽䌐獲樘ྰ㞠戝晐}疽䌐࿈䌐眲Π疽㛏戝癐Π疽伲࿌⒋ფᲉѽ疽䦯䨝抽瘭䦹容㾷碶ᅣᲉᄤྦྷ㜕㞱㗽㾲妴㣗畍⺏'.charCodeAt(--i);alert(o)}

复制/代码粘贴到浏览器控制台,并尝试f('cow','moo')f('pig','oink')f('sheep','baa')

怎么运行的 ?

c是由29个字母和动物及其声音组成的数组(我们称其为字母)。
因此,所有31个字符都适合5位(2 ^ 5 = 32)。
Unicode字符长16位,因此可以用填充位对3个字母进行编码。
新行的全文是我们字母表的 186个字符,可以用62个Unicode字符进行编码。

例如,Old编码如下:

alphabet value         O      l      d
alphabet index         11     20     15
unicode           0  01011  10100  01111  ===> \u2e8f (⺏)

如果您在阅读某些Unicode字符时遇到麻烦,请安装Code2000字体


2
"cow"并且"moo"应该是功能参数。通过使调用方传递数组,可以在函数定义中保存字符,但要增加调用代码的字符数。这闻起来对我来说像个骗子。采取这种方法时,您可以定义function f(a){alert(a)}(23个字符)并说它需要像like那样f("Old MacDonald had a ...")
tobyink 2014年

1
我已经以两个单独的参数传递了字符串,但是在阅读了挑战之后,我没有理由不以数组格式传递字符串,因此我编辑了答案。这不是在作弊,只是在可能的情况下节省了几个字符。让@ rybo111决定是否符合规则。
Michael M.

1
@tobyink我在规则中确实使用了术语“字符串”(不是数组),所以我想你是对的。我认为“作弊”有点苛刻!
rybo111 2014年

2
为什么会有这么多的选票?它超过200个,甚至不是最短的javascript解决方案。
aiditsu 2014年

Whell的思想深思熟虑,我的投票也得到了很好的解释
edc65

5

CJam-142 / GolfScript-144

{" had| a |farm|68, |68 8|here|Old MacDonald765|, E-I-E-I-O|10,
And on that 5 he7690,
With3 2 and3 t2,
Here4t24everyw23,
10!"'|/~A,{`/\*}/}:F;

用法: "cow""moo"F
对于GolfScript替换'|"|",并A10

说明:

关键部分是A,{`/\*}/
A,{...}/对从0到9(A = 10)的每个数字执行该块,
`将数字转换为字符串,然后
/\*执行字符串替换:如果我们在堆栈上,"bar" "foo 1 baz" "1"则将/字符串拆分为["foo " " baz"]\将该数组与上一个项目交换(“ bar”)并*加入数组,结果是"foo bar baz"

因此,代码将主字符串中的每个数字替换为先前在堆栈中的字符串。我们有动物和声音,然后是“ had”,“ a”等,最后是“ EIEIO”,主要字符串是“ 10,...!”。为了避免使用过多的引号,我将所有字符串(参数除外)放在一个字符串中,然后将其拆分并转储所得数组('|/~

主字符串经过以下转换:

10,
And on that 5 he7690,
With3 2 and3 t2,
Here4t24everyw23,
10!

替换"0"", E-I-E-I-O"

1, E-I-E-I-O,
And on that 5 he769, E-I-E-I-O,
With3 2 and3 t2,
Here4t24everyw23,
1, E-I-E-I-O!

替换"1""Old MacDonald765"

Old MacDonald765, E-I-E-I-O,
And on that 5 he769, E-I-E-I-O,
With3 2 and3 t2,
Here4t24everyw23,
Old MacDonald765, E-I-E-I-O!

替换"2""here",然后"3""68 8"etc。

8对应声音,9对应动物。


你能解释一下吗?我甚至不知道什么是CJam
排排坐

@Cruncher CJam是我制作的一种语言,sf.net/p/cjam;一旦打完高尔夫球,我就可以解释一下:)
aittsu 2014年

1
@Cruncher现在添加了解释
-aditsu

9
♬,在这五个他上,六个月九,ze eee ro♬
aiditsu 2014年

可以E-I-是重复的字符串吗?:)
rybo111 2014年

5

Bash + iconv,128个Unicode字符

接受下面的纯bash / ascii函数体并将其反向编码为unicode字符:

m()(c=`iconv -t unicode<<<㵳⁜屡␠ਲ㵨敨敲攊ⰽ⁜ⵅⵉⵅⵉ੏㵯伢摬䴠捡潄慮摬栠摡愠映牡⑭≥攊档␢Ɐ䄊摮漠桴瑡映牡敨栠摡愠␠␱ⱥ圊瑩⑨⁳㈤␠⁨湡⑤⁳㈤琠栤ਬ效敲猤‬⑴⑨ⱳ攠敶祲⑷⑨⁳㈤ਬ漤™ਠ`
eval "${c:2}")

定义一个壳函数m。致电为:

$ m小马邻居
老麦克唐纳(Old MacDonald)有一个农场EIEIO,
在那个农场,他有一只小马EIEIO,
这里有一个邻居,那里有一个邻居,
这里一个邻居,一个邻居,到处都是邻居,
老麦克唐纳有一个农场,EIEIO!
$ 

纯bash,171字节(仅限ASCII)

我认为值得一提的是,原始经文(带有“ cow”和“ moo”)仅为203个字符。

m()(s=\ a\ $2
h=here
e=,\ E-I-E-I-O
o="Old MacDonald had a farm$e"
echo "$o,
And on that farm he had a $1$e,
With$s $2 $h and$s $2 t$h,
Here$s, t$h$s, everyw$h$s $2,
$o"!)

定义shell函数m。致电为:

$ m羊ba
老麦克唐纳(Old MacDonald)有一个农场EIEIO,
在那个农场,他有一只绵羊,EIEIO,
这里有baa baa,那里有baa baa,
这里一个ba,那里一个ba,到处都有一个a,
老麦克唐纳有一个农场,EIEIO!
$

4

C ++(403)

好吧,这有点远,但是谁又不喜欢过分定义呢?

#define O ", E-I-E-I-O"
#define E O<<","
#define I "Old MacDonald had a farm"
#define H(a) "And on that farm he had a "<<a<<E
#define D(s) s<<" "<<s
#define W(s) "With a "<<D(s)<<" here and a "<<D(s)<<" there,"
#define V(s) "Here a "<<s<<", there a "<<s<<", everywhere a "<<D(s)<<","
#define F I<<O<<"!"
#define N endl
void m(string a, string s){cout<<I<<E<<N<<H(a)<<N<<W(s)<<N<<V(s)<<N<<F<<N;}

2
this.eyes.bleeding = true;
代理

有没有定义可以进一步收紧呢?
einsteinsci 2014年

1
曾经有可能#define X define,然后再使用#X Y Z。可悲的是,那些令人
OC目结舌的

那用+代替<<呢?还是用char*代替string?//只能同时使用其中之一。
Qwertiy 2014年

2

Python,116个Unicode字符

def f(**a):print u'鱸쿳光䷰癌쿉ы㊲匒ሔ툕謒畲尔㵵䅵忘쮇⼱ⅅ伿⒡넣Ⰴ邩ઑ꩕醪徜妮ꊌ㰺⒳Ⰳ鮕꾟ౙ㎧譒ᕒ끒镈롴쀼怪㪢愐腤닔ꋔ狊兔Ⲹ㾗꽡Ȩ똀䝸å'.encode('u16')[2:].decode('zip')%a

不过,StackOverflow正在吞噬我的特殊字符,因此这是base64中的文件:

77u/ZGVmIGYoKiphKTpwcmludCB1J+mxuOy/s+WFieS3sOeZjOy/idGL44qy5YyS4YiU7YiV6KyS55Wy5bCU47W15IW15b+Y7K6H4ryx4oWF5Ly/4pKh64Sj4rCE6YKp4KqR6qmV6Yaq5b6c5aau6oqM47C64pKz4rCD6a6V6r6f4LGZ446n6K2S4ZWS74yS64GS6ZWI7pKA66G07IC85oCq46qi5oSQ6IWk64uU6ouU54uK5YWU4rK4476X6r2hyKjrmIDknbjDpScuZW5jb2RlKCd1MTYnKVsyOl0uZGVjb2RlKCd6aXAnKSVh

数据使用zlib打包,zlib有效地编码重复的字符串(zlib通常擅长压缩文本)。为了利用“ Unicode字符”规则,通过将字节字符串解释为UTF-16,来填充121字节的zlib块并将其减半为61个字符的Unicode字符串。

将该函数称为

f(cow='pig', moo='oink')

不错,但是其中Cow和moo是函数参数中的字符串,因此,例如,可以将其更改为pig和oink或绵羊和baa。看起来您的输出已硬编码为cow / moo。
Digital Trauma 2014年

@DigitalTrauma:我的阅读理解失败了!固定。
nneonneo 2014年

那就更好了:) +1
数字创伤

115。无需计算结尾的换行符。
nyuszika7h 2014年

@ nyuszika7h不,是116。您忘了在开始时算“ UTF-8 BOM”(EF BB BF),这是让Python 2接受非ASCII源所必需的。(这不是Python 3,它没有.decode('zip')。)
Anders Kaseorg '16

1

Python,217

你真的不能那么多打高尔夫球。我只是拿出公然的前端重复,然后...

m,f="Old MacDonald had a farm, E-I-E-I-O",lambda x,y:m+",\nAnd on that farm he had a %s, E-I-E-I-O,\nWith a %shere and a %sthere,\nHere a %s, there a %s, everywhere a %s %s,\n%s!"%((x,)+((y+' ')*2,)*2+(y,)*4+(m,))

Javascript 241-JSCrush作弊

使用JSCrush做到了这一点……不是一个真正的答案,只是很有趣的是,看看是否有人可以用主流语言击败它。(编辑:呃)

_='var f=function(c,a){var b=a "+a;return"Anon that he hadcWith  and tHere  t   everyw!"};OlMacDonalhaa a "+, O,\\nhere+"b farm a, d E-I-';for(Y in $=' ')with(_.split($[Y]))_=join(pop());eval(_)

1

爪哇,246

void f(String[] a){String o="Old MacDonald had a farm",e=", E-I-E-I-O",x=" a "+a[1],s=x+" "+a[1];System.out.print(o+e+",\nAnd on that farm he had a "+a[0]+e+",\nWith"+s+" here and"+s+" there,\nHere"+x+", there"+x+", everywhere"+s+",\n"+o+e+"!");}

用法: f(new String[]{"cow","moo"});


1

爪哇- 262 258

void m(String...s){String b=s[1],c=b+" "+b,d="E-I-E-I-O",e="Old MacDonald had a farm, "+d;System.out.print(e+",\n"+"And on that farm he had a "+s[0]+", "+d+",\nWith a "+c+" here and a "+c+" there,\nHere a "+b+", there a "+b+", everywhere a "+c+",\n"+e+"!");}

进一步优化无疑是可能的。


您可以使用printf
aditsu

Trevin Avery建议进行以下编辑:Java-243-void String ... a){String c =“ a” + a [1],d = c +“” + a [1],e =“,EIEIO”,f = “老麦克唐纳有一个农场” + e; System.out.print(f +“,\ n在那个农场上他有一个” + a [0] + e +“,\ n这里有” + d +“,那里有” + d +“ ,\ n这里“ + c +”,那里“ + c +”',无处不在“ + d +”,\ n“ + f +”!“);}肯定可以进行进一步的优化
贾斯汀

1

Perl 5(UTF-8)-131个字符,313个字节

以下脚本需要另存为UTF-8,且不包含BOM。

use utf8;use Encode;eval encode ucs2,'獵戠晻③㴤∮灯瀻⑥㴢Ⱐ䔭䤭䔭䤭伢㬤漽≏汤⁍慣䑯湡汤⁨慤⁡⁦慲洤攢㬤ⰽ≥牥⁡∻獡礢⑯Ⰺ䅮搠潮⁴桡琠晡牭⁨攠桡搠愠䁟⑥Ⰺ坩瑨⁡③③⁨␬湤⁡③③⁴桥牥Ⰺ䠤Ⱔ戬⁴栤Ⱔ戬⁥癥特睨␬③③Ⰺ⑯™紱';

用法:f("cow", "moo");

-M5.010要启用Perl 5.10功能,需要使用该标志运行Perl。(这是允许的。)

我非常喜欢字符数(131)和字节数(313)的对称性。非常阴和阳。

Perl 5(ASCII)-181个字符,181个字节

sub f{$b=$".pop;$e=", E-I-E-I-O";$o="Old MacDonald had a farm$e";$,="ere a";say"$o,
And on that farm he had a @_$e,
With a$b$b h$,nd a$b$b there,
H$,$b, th$,$b, everywh$,$b$b,
$o!"}

用法:f("cow", "moo");

同样,需要使用该-M5.010标志运行perl 以启用Perl 5.10功能。



其实codegolf.stackexchange.com/a/26628/12469是我的出发点。我测试了一些额外的变量,这些变量进一步缩短了长度,然后应用了其他一些实现所利用的UTF16技巧。
tobyink 2014年

1

CJam(非ASCII)-77个字符

"啝裢樃濿䶹讄團챤鋚䖧雿ꆪꆵ䷶텸紎腕Խꍰ搓᩟童䚯⤭刧损⬛豳Ẍ퍾퓱郦퉰怈䡞௳閶蚇⡾쇛蕟猲禼࿆艹蹚㞿䛴麅鞑椢⧨餎쏡첦휽嬴힡ݷ녣㯂鐸㭕"56e3b127b:c~

用法: "cow""moo"F

该字符串是我的其他CJam解决方案,它从base 127转换为base56000
。可能需要UTF-8语言环境。

顺便说一句,现在您可以在http://cjam.aditsu.net/上在线尝试此操作


1

JavaScript:152个字符/ ES6:149个字符

这是一个称为“ z”的JS函数,它以214个字符的形式完成工作。(不要执行!)

function z(a,b){c=' a '+b;d=c+' '+b;e=', E-I-E-I-O';f='Old MacDonald had a farm'+e;return(f+',\nAnd on that farm he had a '+a+e+',\nWith'+d+' here and'+d+' there,\nHere'+c+', there'+c+', everywhere'+d+',\n'+f+'!')}

我使用@subzey和我为140byt.es创建的技术将其“打包”为Unicode字符。

eval(unescape(escape('𩡵𫡣𭁩𫱮𘁺𚁡𛁢𚑻𨰽𙰠𨐠𙰫𨠻𩀽𨰫𙰠𙰫𨠻𩐽𙰬𘁅𛑉𛑅𛑉𛑏𙰻𩠽𙱏𫁤𘁍𨑣𡁯𫡡𫁤𘁨𨑤𘁡𘁦𨑲𫐧𚱥𞱲𩑴𭑲𫠨𩠫𙰬𧁮𠑮𩀠𫱮𘁴𪁡𭀠𩡡𬡭𘁨𩐠𪁡𩀠𨐠𙰫𨐫𩐫𙰬𧁮𥱩𭁨𙰫𩀫𙰠𪁥𬡥𘁡𫡤𙰫𩀫𙰠𭁨𩑲𩐬𧁮𢁥𬡥𙰫𨰫𙰬𘁴𪁥𬡥𙰫𨰫𙰬𘁥𭡥𬡹𭱨𩑲𩐧𚱤𚰧𛁜𫠧𚱦𚰧𘐧𚑽').replace(/uD./g,'')))

执行最后一个代码段,然后调用z("cow","moo"),您将获得以下字符串:

Old MacDonald had a farm, E-I-E-I-O,
And on that farm he had a cow, E-I-E-I-O
With a moo moo here and a moo moo there,
Here a moo, there a moo, everywhere a moo moo,
Old MacDonald had a farm, E-I-E-I-O!"

此处提供更多信息:http : //xem.github.io/golfing/en.html#compress

ES6版本:

eval(unescape(escape('𮠽𚁡𛁢𚐽🡻𨰽𙰠𨐠𙰫𨠻𩀽𨰫𙰠𙰫𨠻𩐽𙰬𘁅𛑉𛑅𛑉𛑏𙰻𩠽𙱏𫁤𘁍𨑣𡁯𫡡𫁤𘁨𨑤𘁡𘁦𨑲𫐧𚱥𞱲𩑴𭑲𫠨𩠫𙰬𧁮𠑮𩀠𫱮𘁴𪁡𭀠𩡡𬡭𘁨𩐠𪁡𩀠𨐠𙰫𨐫𩐫𙰬𧁮𥱩𭁨𙰫𩀫𙰠𪁥𬡥𘁡𫡤𙰫𩀫𙰠𭁨𩑲𩐬𧁮𢁥𬡥𙰫𨰫𙰬𘁴𪁥𬡥𙰫𨰫𙰬𘁥𭡥𬡹𭱨𩑲𩐧𚱤𚰧𛁜𫠧𚱦𚰧𘐧𚑽').replace(/uD./g,'')))

我认为您没有正确地复制粘贴内容,您的代码似乎超过了250个字符-哎呀,也许不是,但是我的文本编辑器的行为很奇怪,我将进行调查。
2014年

哦,您的大多数字符都来自某些星体平面(这可能就是为什么它们在这里被算作2个字符的原因)...而且也没有分配。这有点
延长

好吧,我不认为这是骗人的:这些符号是unicode字符,不应计为2个字符。另外,Twitter将它们每个都计为1个字符。如果您在推文中复制ES6版本,则说明它太长了9个字符。因此,它是149 :)
xem

1

C#-339字节

void x(string c, string d){var a="Old MacDonald had a farm";var b=", E-I-E-I-O";var f=" a ";var g=" there";Debug.WriteLine(a+b+",");Debug.WriteLine("And on that farm he had"+f+c+b+",");Debug.WriteLine("With"+f+d+" "+d+" here and"+f+d+" "+d+g+",");Debug.WriteLine("Here"+f+d+","+g+f+d+", everywhere"+f+d+" "+d+",");Debug.WriteLine(a+b+"!");

用法: x("cow","moo");


1

雷博尔,206 202

f: func[a b][print reword{$o$e,
And on that farm he had a $a$e,
With a $b $b here and a $b $b there,
Here a $b, there a $b, everywhere a $b $b,
$o$e!}[e", E-I-E-I-O"o"Old MacDonald had a farm"a a b b]]

用法: f "cow" "moo"


0

Delphi的XE3(272 252)

procedure k(a,s:string);const o='Old MacDonald had a farm';e=', E-I-E-I-O';n=','#13#10;begin s:=' '+s;write(o+e+n+'And on that farm he had a '+a+e+n+'With a'+s+s+' here and a'+s+s+' there'+n+'Here a'+s+', there a'+s+' every where a'+s+s+n+o+e+'!');end;

不打高尔夫球

procedure k(a,s:string);
const
  o='Old MacDonald had a farm';
  e=', E-I-E-I-O';
  n=','#13#10;
begin
  s:=' '+s;
  write(o+e+n+'And on that farm he had a '+a+e+n+'With a'+s+s+' here and a'+s+s+' there'+n+'Here a'+s+', there a'+s+' every where a'+s+s+n+o+e+'!');
end;

0

卢阿237

function f(a,b)c=b.." "..b;d="Old MacDonald had a farm, E-I-E-I-O"print(d..",\nAnd on that farm he had a "..a..", E-I-E-I-O,\nWith a "..c.." here and a "..c.." there,\nHere a "..b..", there a "..b..", everywhere a "..c..",\n"..d.."!")end

通过定义c=b.." "..b,我可以保存许多字符。通过定义d,我节省了23个字符。我看不到如何可以缩短它。这称为通过f("<animal>","<sound>")


0

爪哇8(411)

String m(String...m){LinkedHashMap<String,String>n=new LinkedHashMap<>();n.put("/","( * #, -");n.put("#","farm");n.put("-","E-I-E-I-O");n.put("+","here");n.put("*","had a");n.put("(","Old MacDonald");n.put("|"," a )");n.put(")","moo");n.put("moo",m[1]);n.put("cow",m[0]);m[0]="/,\nAnd on that # he * cow, -,\nWith|) + and|) t+,\nHere|, t+|, everyw+|),\n/!";n.forEach((k,v)->m[0]=m[0].replace(k,v));return m[0];}

滥用lambda,将替换放置在LinkedhashMap中,以使em保持定义的顺序,然后使用foreach lambda将键替换为主String中的值。参数被添加为地图中的最后2个替换项。varargs参数是要删除方法标题中的某些字节

非高尔夫版本:

String m(String... m)
{
    LinkedHashMap<String, String> n = new LinkedHashMap<>();
    n.put("/", "( * #, -");
    n.put("#", "farm");
    n.put("-", "E-I-E-I-O");
    n.put("+", "here");
    n.put("*", "had a");
    n.put("(", "Old MacDonald");
    n.put("|", " a )");
    n.put(")", "moo");
    n.put("moo", m[1]);
    n.put("cow", m[0]);
    m[0] = "/,\nAnd on that # he * cow, -,\nWith|) + and|) t+,\nHere|, t+|, everyw+|),\n/!";
    n.forEach((k, v) -> m[0] = m[0].replace(k, v));
    return m[0];
}

0

JavaScript 220

function f(a,b){c=' a '+b;d=c+' '+b;e=', E-I-E-I-O';f='Old MacDonald had a farm'+e;console.log(f+',\nAnd on that farm he had a '+a+e+',\nWith'+d+' here and'+d+' there,\nHere'+c+', there'+c+', everywhere'+d+',\n'+f+'!');}

致电者

f('cow', 'moo');

0

纯C,298字节,无unicode

在我的函数中,我采用一个参数,实际上是一堆char*打包在一起的。每个字符串都以null终止,并且在末尾还有一个额外的null终止符。这使我可以strlen(a)在每个循环的末尾进行检查,而不必保留计数器变量。

mcdonald.c:

m(char*a){while(strlen(a)){printf("Old MacDonald had a farm, E-I-E-I-O\nAnd on that farm he had a %s, E-I-E-I-O,\nWith a ",a);a+=strlen(a)+1;printf("%s %s here and a %s %s there,\nHere a %s, there a %s, everywhere a %s %s,\nOld MacDonald had a farm, E-I-E-I-O!\n",a,a,a,a,a,a,a,a);a+=strlen(a)+1;}}

main.c:

int m(char *v);
int main(int argc, char **argv) {
    m("cow\0moo\0programmer\0meh\0\0");
    return 0;
}

输出:

clang main.c mcdonald.c && ./a.out
Old MacDonald had a farm, E-I-E-I-O
And on that farm he had a cow, E-I-E-I-O,
With a moo moo here and a moo moo there,
Here a moo, there a moo, everywhere a moo moo,
Old MacDonald had a farm, E-I-E-I-O!
Old MacDonald had a farm, E-I-E-I-O
And on that farm he had a programmer, E-I-E-I-O,
With a meh meh here and a meh meh there,
Here a meh, there a meh, everywhere a meh meh,
Old MacDonald had a farm, E-I-E-I-O!

0

眼镜蛇-203

def f(a,b)
    d=" a [b] "+b
    e=", E-I-E-I-O"
    m="Old MacDonald had a farm[e]"
    print "[m],\nAnd on that farm he had a [a][e],\nWith[d] here and[d] there,\nHere a [b], there a [b], everywhere[d],\n[m]!"

即使对于几乎没有甚至没有多行代码且严格的缩进规则的语言,Cobra仍然表现出色。


0

C:224个字节

通过使用printf精度说明符,我们可以将相同的字符串用作printf格式字符串和两个参数。

o(char*x,char*y){char*f="Old MacDonald had a farm, E-I-E-I-O,\nAnd on that farm he had a %s%.13sWith a %s %s here and a %s %s there,\nHere a %s, there a %s, everywhere a %s %s,\n%.35s!\n";printf(f,x,f+24,y,y,y,y,y,y,y,y,f);}

用空格和字符串分成几行:

o(char* x, char* y)
{
    char* f=
        "Old MacDonald had a farm, E-I-E-I-O,\n"
        "And on that farm he had a %s%.13s"
        "With a %s %s here and a %s %s there,\n"
        "Here a %s, there a %s, everywhere a %s %s,\n"
        "%.35s!\n";

    printf(f,x,f+24,y,y,y,y,y,y,y,y,f);
}

0

PHP-272个字符,272个字节

function m($q,$w){for($e="@&And on that farm he had^<%&With *h# and*th#&H(th(everywh#^> >&@!",$r=1;;$e=$r){$r=str_replace(["@","#","^","%","<",">","&","*","("],["Old MacDonald had^farm%","ere"," a ",", E-I-E-I-O",$q,$w,",\n","^> > ","#^>, "],$e);if($e==$r)break;}echo $e;}

用途:m("cow", "moo");m("fox", "Hatee-hatee-hatee-ho");

带参数@#%^<>&*(的输出崩溃。


0

Haskell(282,但仍然有点可读:)

wc -c oldmacdonald.hs
     282 oldmacdonald.hs

文件:

main=mapM putStrLn[s"cow""moo",s"pig""oink",s"sheep""baa"]
s c m=o#",\nAnd on that farm he had"#b c#e#let n=m#" "#m in",\nWith"#b n#" here and"#b n#" there,\nHere"#b m#", there"#b m#", everywhere"#b n#",\n"#o#"!\n"
o="Old MacDonald had a farm"#e
e=", E-I-E-I-O"
b=(" a "#)
(#)=(++)

它是281,通常不会计算结尾的换行符,除非它是C预处理程序宏或其他需要换行符终止的东西。在大多数情况下,您可以从所返回的字节数中减去1 wc -c,但我更喜欢使用mothereff.in/byte-counter并确保最后没有空行,除非程序正常运行。
nyuszika7h 2014年

0

ES6,2个解决方案,共179186个字符,无任何Unicode

f=(a,b)=>alert("325And on that farm he had a025With a11 h4nd a11 th45H41, th41, everywh411532!".replace(/\d/g,x=>[" "+a," "+b,", E-I-E-I-O","Old MacDonald had a farm","ere a",",\n"][x]))

第二个:

f=(a,b)=>alert("3625And on7at6 he ha8025With a11 h4n811745H41,741, everywh4115362!".replace(/\d/g,x=>(` ${a}0 ${b}0, E-I-E-I-O0Old MacDonald had a0ere a0,\n0 farm0 th0d a`).split(0)[x]))

我添加了提醒电话(+7个字符)。


我认为,这是目前非非编码解决方案中最短的解决方案。
Qwertiy 2014年

0

JavaScript(E6)140个字符

字符计数器: https //mothereff.in/byte-counter个字符,425字节(UTF-8)

eval(unescape(escape('𩠽𚁡𛁢𚐽🡡𫁥𬡴𚀧𜀱𜠵𠑮𩀠𫱮𘀶𨑴𜐳𩐳𨑤𝰠𞐲𝑗𪐶𘁡𞀸𜰴𝱮𩀠𨐸𞀠𝠴𝑈𝀷𞀬𘀶𝀷𞀬𘁥𭡥𬡹𭱨𝀷𞀸𝐰𜐲𘐧𛡲𩑰𫁡𨱥𚀯𧁤𛱧𛁣🐾𚀧𣱬𩀠𣑡𨱄𫱮𨑬𩀠𪁡𩀠𨐰𘁦𨑲𫐰𛀠𡐭𢐭𡐭𢐭𣰰𘁨𜁥𬡥𜀬𧁮𜁴𪀰𘁡𜀠𙰫𨠫𜀫𨐩𛡳𬁬𪑴𚀰𚑛𨱝𚐩𒠠').replace(/uD./g,'')))

原始ASCII码188字节

f=(a,b)=>alert('0125And on 6at13e3ad7 925Wi6 a88347nd a88 645H478, 6478, everywh47885012!'.replace(/\d/g,c=>('Old MacDonald had a0 farm0, E-I-E-I-O0 h0ere0,\n0th0 a0 '+b+0+a).split(0)[c]))

压缩有 http://xem.github.io/obfuscatweet/

在FireFox / FireBug控制台中测试

f('mosquito','zzz')

输出量

Old MacDonald had a farm, E-I-E-I-O,
And on that farm he had a mosquito, E-I-E-I-O,
With a zzz zzz here and a zzz zzz there,
Here a zzz, there a zzz, everywhere a zzz zzz,
Old MacDonald had a farm, E-I-E-I-O!
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.