打印popular holiday song的所有12节经文。
12个经文我指的是歌曲中每首重复的行,即
诗歌一: 在圣诞节的第一天,我的真爱给了我 梨树上的鹧。。
第二节 在圣诞节的第二天,我的真爱给了我 两只乌龟鸽子 和梨树上的鹧。。
...
诗歌N: 在圣诞节的第n天,我的真爱给了我 (没有第一行的第N-1节) (在第N节中添加的行)
答案 0 :(得分:44)
Common Lisp:
(mapc #'princ
(reverse (maplist #'(lambda(l)
(format nil
"On the ~:R day of Christmas my true love gave to me~%~{~a~%~}~%"
(length l) l))
'("twelve drummers drumming,"
"eleven pipers piping,"
"ten lords a-leaping,"
"nine ladies dancing,"
"eight maids a-milking,"
"seven swans a-swimming,"
"six geese a-laying,"
"five gold rings,"
"four calling birds,"
"three french hens,"
"two turtle doves, and"
"a partridge in a pear tree."))))
编辑:
如果你拿出空白,上面是412个字符。
这一个:
(let ((g))
(dotimes (i 12)
(format t
"On the ~:R day of Christmas my true love gave to me~%~{~R ~:*~
~[~;~;turtle doves and~;french hens,~;calling birds,~;gold rings,~
~;geese a-laying,~;swans a-swimming,~;maids a-milking,~
~;ladies dancing,~;lords a-leaping,~;pipers piping,~
~;drummers drumming,~]~%~}a partridge in a pear tree~2%"
(1+ i) g)
(push (+ i 2) g)))
如果你在格式字符串中取出空格和〜引用的换行符,是344个字符:
(let((g))(dotimes(i 12)(format t"On the ~:R day of Christmas my true love gave to me~%~{~R ~:*~[~;~;turtle doves and~;french hens,~;calling birds,~;gold rings,~;geese a-laying,~;swans a-swimming,~;maids a-milking,~;ladies dancing,~;lords a-leaping,~;pipers piping,~;drummers drumming,~]~%~}a partridge in a pear tree~2%"(1+ i)g)(push(+ i 2)g)))
编辑:
看起来问题已经解决了,网站正在唠叨我接受答案。据我所知,这个是最短的。如果我接受自己的答案,我有点害怕网站将会做什么 - 可能会给我一个自恋者或自慰者徽章。
您无法接受自己的答案。很公平。我会把它打开。感谢所有回复的人。
答案 1 :(得分:37)
使用F#:
#light
open System.Net; open System.Text.RegularExpressions
printf "%s" ((new WebClient()).DownloadString("http://www.textfiles.com/holiday/12-bugs")
|> (fun x -> (new Regex("Lines: \d+\s+([\s\S]+)--")).Match(x).Groups.[1].Value))
第二天产出:
For the twelfth bug of Christmas, my manager said to me Tell them it's a feature Say it's not supported Change the documentation Blame it on the hardware Find a way around it Say they need an upgrade Reinstall the software Ask for a dump Run with the debugger Try to reproduce it Ask them how they did it and See if they can do it again.
答案 2 :(得分:27)
不是我的,但有趣......
这是来自http://en.wikipedia.org/wiki/Obfuscated_code的着名例子(作者:James O. Coplien):
#include <stdio.h>
main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
“虽然乍一看是难以理解的,但它是一个合法的C程序,在编译和运行时将生成圣诞节12天的12节经文。它包含了代码内联编码形式的诗所需的所有字符串。代码在12天内迭代显示它需要的内容。“
虽然它不会赢得高尔夫,但它大约是830字节。
答案 3 :(得分:15)
在D编程语言中,使用switch语句直通:
import std.stdio;
void main() {
for(uint i = 1; i < 13; i++) {
writeln("On the ", i, " day of Christmas, my true love gave to me:");
switch(i) {
case 12:
writeln("twelve drummers drumming,");
case 11:
writeln("eleven pipers piping,");
case 10:
writeln("ten lords a-leaping,");
case 9:
writeln("nine ladies dancing,");
case 8:
writeln("eight maids a-milking,");
case 7:
writeln("seven swans a-swimming,");
case 6:
writeln("six geese a-laying,");
case 5:
writeln("five gold rings,");
case 4:
writeln("four calling birds,");
case 3:
writeln("three french hens,");
case 2:
writeln("two turtle doves, and");
case 1:
writeln("a partridge in a pear tree.\n");
}
}
}
答案 4 :(得分:15)
class TrueLove: ITrueLove
{
List<IPresent> give(int day)
{
List<IPresent> lovesLabourLost = new List<IPresent>();
//if this was C++ could you replace this with a Figgy Duff Device?
for (int i=1; i<=day; i++)
{
if(day > 1 && i<=2) {lovesLabourLost .Add(new TurtleDove());}
if(day > 2 && i<=3) {lovesLabourLost .Add(new FrenchHen());}
if(day > 3 && i<=4) {lovesLabourLost .Add(new CallingBird());}
if(day > 4 && i<=5) {lovesLabourLost .Add(new GOLDRING());}
if(day > 5 && i<=6) {lovesLabourLost .Add(new LayingGeese());}
if(day > 6 && i<=7) {lovesLabourLost .Add(new SwimmingSwan());}
if(day > 7 && i<=8) {lovesLabourLost .Add(new MilikingMaid());}
if(day > 8 && i<=9) {lovesLabourLost .Add(new DancingLady());}
if(day > 9 && i<=10) {lovesLabourLost .Add(new LeapingLord());}
if(day > 10 && i<=11) {lovesLabourLost .Add(new PipingPiper());}
if(day > 11 && i<=12) {lovesLabourLost .Add(new DrummingDrummer());}
}
return lovesLabourLost && (new PartridgeInPearTree());
}
}
static class Me: IDemanding
{
static ITrueLove myTrueLove = new TrueLove();
static List<IPresent> myPresents = new List<IPresent>();
static void demandPresents()
{
for (int i=1; i<=daysOfChristmas; i++)
{
List<IPresent> MOAR = myTrueLove.give(i);
foreach (IPresent another in MOAR)
{
myPresents.Add(another);
}
}
}
}
const int daysOfChristmas = 12;
Me.demandPresents();
答案 5 :(得分:14)
在C ++中你是
std::cout << boost::12_days_of_christmas;
答案 6 :(得分:14)
答案 7 :(得分:11)
var t="";for(int i=0;i++<12;)Console.Write("On the {0}{1} day of Christmas, my true love gave to me: {2}\n",i,i<2?"st":i<3?"nd":i<4?"rd":"th",t="|a partridge in a pear tree.|two turtle doves, and |three french hens,|four calling birds,|five gold rings|six geese a-lay@seven swans a-swimm@eight maids a-milk@nine ladies danc@ten lords a-leap@eleven pipers pip@twelve drummers drumm@".Replace("@","ing,|").Split('|')[i]+t);
var t="";
for(int i = 0; i++ < 12;)
Console.Write("On the {0}{1} day of Christmas, my true love gave to me: {2}\n",
i,
i < 2 ? "st" : i < 3 ? "nd" : i < 4 ? "rd" : "th",
t="|a partridge in a pear tree.
|two turtle doves, and
|three french hens,
|four calling birds,
|five gold rings
|six geese a-lay
@seven swans a-swimm
@eight maids a-milk
@nine ladies danc
@ten lords a-leap
@eleven pipers pip
@twelve drummers drumm@"
.Replace("@","ing,|")
.Split('|')[i]+t);
答案 8 :(得分:9)
$v=split(":",":a partridge in a pear tree.\n:two turtle doves, and:three french hens:four calling birds:five gold rings:six geese a-lay:seven swans a-swimm:eigth maids a-milk:nine ladies danc:ten lords a-leap:eleven pipers pip:twelve drummers drumm");while($i<12){?>On the <?=date(jS,$i*86400)," day of Christmas my true love gave to me",$s=", ".$v[++$i].($i>5?'ing':'').$s;}
编辑:更新的可运行版本(377)字符
<?$v=split(":",":a partridge in a pear tree.\n:two turtle doves, and:three french hens:four calling birds:five gold rings:six geese a-lay:seven swans a-swimm:eigth maids a-milk:nine ladies danc:ten lords a-leap:eleven pipers pip:twelve drummers drumm");while($i<12){?>On the <?=date(jS,$i*86400)," day of Christmas my true love gave to me",$s=", ".$v[++$i].($i>5?'ing':'').$s;}
答案 9 :(得分:7)
Linq to objects,580个字符(没有空格)
Console.WriteLine(Enumerable.Range(1, 13).SelectMany(day =>
Enumerable.Repeat("\nOn the " + day +
(day == 1 ? "st" :
(day == 2 ? "nd" :
(day == 3 ? "rd" :
"th"))) + " day of Christmas my true love gave to me ", 1)
.Concat((new []
{
"twelve drummers drumming,",
"eleven pipers piping,",
"ten lords a-leaping,",
"nine ladies dancing,",
"eight maids a-milking,",
"seven swans a-swimming,",
"six geese a-laying,",
"five gold rings,",
"four calling birds,",
"three french hens,",
"two turtle doves, and",
"a partridge in a pear tree."
}).Reverse().Take(day).Reverse()))
.Aggregate((a, b) => a + "\n" + b));
答案 10 :(得分:7)
VB.Net - 530个字符(无空格),634(空格)
Module ChristmasSong
Sub Main()
Dim i&, f$ : Dim d$() = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"}, g$() = {"a partridge in a pear tree.", "two turtle doves, and ", "three french hens, ", "four calling birds, ", "five gold rings, ", "six geese a-laying, ", "seven swans a-swimming, ", "eigth maids a-milking, ", "nine ladies dancing, ", "ten lords a-leaping, ", "eleven pipers piping, ", "twelve drummers drumming, "}
For i = 0 To 11 : f = g(i) & f : Console.WriteLine("On the {0} day of Christmas, my true love gave to me {1}", d(i), f) : Next
End Sub
End Module
答案 11 :(得分:6)
的Perl。
use Lingua::EN::Numbers qw(num2en_ordinal);
print 'On the ', num2en_ordinal($_+1),' day of Christmas my true love gave to me, ', reverse(( split /\|/, "a partridge in a pear tree.\n|two turtle doves, and |three french hens, |four calling birds, |five gold rings, |six geese a-laying, |seven swans a-swimming, |eight maids a-milking, |nine ladies dancing, |ten lords a leaping, |eleven pipers piping, |twelve drummers drumming, ")[ 0 .. $_ ]) for 0 .. 11;
(459 Chars)
我想让它变得更好,更富有表现力,但这是高尔夫挑战。
这种风格在某种程度上违反了正确的编码标准。但那对你来说是高尔夫球。
这是不那么紧凑的版本,而且不那么狡猾的技巧。
use Lingua::EN::Numbers qw(num2en_ordinal);
my @gifts = (
'a partridge in a pear tree.',
'two turtle doves, and ',
'three french hens, ',
'four calling birds, ',
'five gold rings, ',
'six geese a-laying, ',
'seven swans a-swimming, ',
'eight maids a-milking, ',
'nine ladies dancing, ',
'ten lords a leaping, ',
'eleven pipers piping, ',
'twelve drummers drumming, '
);
for my $verse_id ( 0 .. $#gifts ) {
printf 'On the %s day of Christmas my true love gave to me, ', num2en_ordinal($verse_id +1);
print reverse @verse[ 0 .. $verse_id ];
print "\n";
}
答案 12 :(得分:6)
'这是季节! (而且非常合适,因为LilyPond主要用于排版音乐......除了歌词之外,人们可以很容易地调整这段代码来为这首歌创作乐谱。)
改编自fizzer的解决方案。
#(map(lambda(x)(format #t"On the ~:R day of Christmas my true love gave to me~{ ~R~:*~[~;~;turtle doves and~;French hens,~;calling birds,~;gold rings,~;geese a-laying,~;swans a-swimming,~;maids a-milking,~;ladies dancing,~;lords a-leaping,~;pipers piping,~;drummers drumming,~]~} a partridge in a pear tree.
"x(iota(1- x)x -1)))(iota 12 1))
用法:$ lilypond thisfile.ly
此版本以341个字符完全匹配fizzer的输出,但短三个字节:
#(map(lambda(x)(format #t"On the ~:R day of Christmas my true love gave to me~{
~R ~:*~[~;~;turtle doves and~;french hens,~;calling birds,~;gold rings,~;geese a-laying,~;swans a-swimming,~;maids a-milking,~;ladies dancing,~;lords a-leaping,~;pipers piping,~;drummers drumming,~]~}
a partridge in a pear tree
"x(iota(1- x)x -1)))(iota 12 1))
答案 13 :(得分:6)
for d in range(12):print"On the %s day of Christmas, my true love gave to me\n\t%s\n"%("first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelfth".split("|")[d],"\n\t".join("twelve drummers drumming|eleven pipers piping|ten lords a-leaping|nine ladies dancing|eight maids a-milking|seven swans a-swimming|six geese a-laying|five gold rings|four calling birds|three french hens|two turtle doves and|a partridge in a pear tree.".split("|")[11-d:]))
Python,422个字符
答案 14 :(得分:5)
红宝石。我认为这非常简洁(努力工作都在一条线上):
days = %w{First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth}
presents = [
"twelve drummers drumming",
"eleven pipers piping",
"ten lords a leaping",
"nine ladies dancing",
"eight maids a-milking",
"seven swans a-swimming",
"six geese a-laying",
"five gold rings",
"four calling birds",
"three french hens",
"two turtle doves, and",
"a partridge in a pear tree"
]
0.upto(11) { |i|
puts "On the #{days[i]} of Christmas my true love gave to me " + presents.last(i+1).join(", ")
}
答案 15 :(得分:4)
perl -MTemplate -e 'Template->new()->process("12dayxmas.tt")'
[% list = [ { day => 'first', item => 'A partridge in a pear tree.'}, { day => 'second', item => 'Two turtle doves, and '}, { day => 'third', item => 'Three french hens, '}, { day => 'fourth', item => 'Four calling birds, '}, { day => 'fifth', item => 'Five gold rings, '}, { day => 'sixth', item => 'Six geese a-laying, '}, { day => 'seventh', item => 'Seven swans a-swimming, '}, { day => 'eighth', item => 'Eight maids a-milking, '}, { day => 'nineth', item => 'Nine ladies dancing, '}, { day => 'tenth', item => 'Ten lords a-leaping, '}, { day => 'eleventh', item => 'Eleven pipers piping, '}, { day => 'twelfth', item => 'Twelve drummers drumming, '} ]; -%] [% FOREACH list; present = item _ present; -%] On the [% day %] day of Christmas my true love gave me [% present %] [% END %]
答案 16 :(得分:4)
这是一个PHP解决方案:
$day = Array('first','second','third','fourth','fifth','sixth','seventh',
'eighth','ninth','tenth','eleventh','twelfth');
$gifts = Array('Twelve drummers drumming,',
'Eleven pipers piping,',
'Ten lords a-leaping,',
'Nine ladies dancing,',
'Eight maids a-milking,',
'Seven swans a-swimming,',
'Six geese a-laying,',
'FIVE GOLDEN RINGS,',
'Four calling birds,',
'Three French hens,',
'Two turtle doves, and',
'A partridge in a pear tree');
for ($i = 0; $i < 12; ++$i) {
printf("On the $day[$i] of Christmas my true love gave to me\n" .
implode("\n", array_slice($gifts,-($i+1))) . "\n\n");
}
答案 17 :(得分:4)
h
在我的虚拟语言中,命令h
打印“Hello,World!”。哦,等等,你的意思是,这不是我们所说的?
答案 18 :(得分:4)
import sys
sys.path += ["TDOC.zip"]
import TDOC
TDOC.print_verse()
TDOC.zip(467字节)包含TDOC.py:
def print_verse(presents="""\
twelve drummers drumming,
eleven pipers piping,
ten lords a-leaping,
nine ladies dancing,
eigth maids a-milking,
seven swans a-swimming,
six geese a-laying,
five gold rings,
four calling birds,
three french hens,
two turtle doves, and
a partridge in a pear tree.""".split("\n")):
if presents:
print_verse(presents[1:])
number = presents[0].split(" ", 1)[0]
print("On the %s day of Christmas my true love gave to me %s" % (
dict(a="first", two="second", three="third", five="fifth").get(number, number+"th"),
" ".join(presents)))
答案 19 :(得分:3)
答案 20 :(得分:3)
C#:
string[] s = new string[]{
"a partridge in a pear tree.",
"two turtle doves, and ",
"three french hens, ",
"four calling birds, ",
"five gold rings, ",
"six geese a-laying, ",
"seven swans a-swimming, ",
"eight maids a-milking, ",
"nine ladies dancing, ",
"ten lords a-leaping, ",
"eleven pipers piping, ",
"twelve drummers drumming, "
};
string t = "";
for (int x = 0; x < s.Length; x++) {
t = s[x] + t;
Console.Write("On the "
+ (x + 1).ToString()
+ (x == 0 ? "st" : (x == 1 ? "nd" : (x == 2 ? "rd" : "th")))
+ " day of christmas, my true love gave to me: " + t + "\n");
}
574个字符,不计算缩进。添加一些额外的字符以使数字扩展正确。不过可能会有相当大的改进。
答案 21 :(得分:3)
PHP
$g = array("a partridge in a pear tree.\n",
"two turtle doves, and",
"three french hens,",
"four calling birds,",
"five gold rings,",
"six geese a-laying,",
"seven swans a-swimming,",
"eight maids a-milking,",
"nine ladies dancing,",
"ten lords a-leaping,",
"eleven pipers piping,",
"twelve drummers drumming,"
);
$d = array("first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "nineth", "tenth", "eleventh", "twelfth");
foreach($d as $i=>$v){
echo "On the $v day of Christmas my true love gave to me";
for($j=$i;$j>=0;$j--) echo " ",$g[$j];
}
答案 22 :(得分:3)
红宝石
a=["and a partridge in a pear tree","turtle doves","French hens","calling birds","golden rings","geese a-laying","swans a-swimming","maids a-milking","ladies dancing","lords a-leaping","pipers piping","drummers drumming"]
b=["","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"]
c=["first","second","third","fourth","fifth","sixth","seventh","eighth","nineth","tenth","eleventh","twelfth"]
0.upto(11){|d|puts "On the "+c[d]+" day of Christmas, my true love gave to me:\n"+b[d]+" "+a[d]+",";(d-1).downto(0){|e| f=", ";f = "." if e==0;puts b[e]+" "+a[e]+f;}}
总计:使用UNIX LF的593字节。
答案 23 :(得分:3)
我无法击败Lisp版本,但它仍然很有趣。
Delphi版本:
procedure TheTwelfDaysOfChristmas(const AVerse: TStrings);
const
cPresentList : array[1..12] of string = (
'a partridge in a pear tree',
'two turtle doves, and ',
'three french hens, ',
'four calling birds, ',
'five gold rings, ',
'six geese a-laying, ',
'seven swans a-swimming, ',
'eigth maids a-milking, ',
'nine ladies dancing, ',
'ten lords a-leaping, ',
'eleven pipers piping, ',
'twelve drummers drumming, '
);
cTime : array[1..12] of string = (
'first',
'second',
'third',
'fourth',
'fifth',
'sixth',
'seventh',
'eighth',
'nineth',
'tenth',
'eleventh',
'twelfth'
);
var
present : string;
i : Integer;
begin
present := '';
for i := 1 to 12 do begin
present := cPresentList[i] + present;
AVerse.Add(Format('On the %s day of Christmas my true love gave me %s.',
[cTime[i], present]));
end;
end;
顺便说一句,对于你们所有人来说,2009年的节日快乐和精彩的节目。
答案 24 :(得分:3)
C#
string[] days = new string[] {"First",
"Second", "Third", "Fourth", "Fifth", "Sixth",
"Seventh", "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth"};
string[] presents = new string[] {"a partridge in a pear tree.",
"two turtle doves, and",
"three french hens,",
"four calling birds,",
"five gold rings,",
"six geese a-laying,",
"seven swans a-swimming,",
"eigth maids a-milking,",
"nine ladies dancing,",
"ten lords a-leaping,",
"eleven pipers piping,",
"twelve drummers drumming,"};
int cnt =0;
foreach (string s in presents)
{
Console.WriteLine(string.Format("On the {0} day of Christmas my true love gave to me", days[cnt++]));
foreach (string p in presents.Take(cnt).Reverse())
Console.WriteLine(p);
Console.WriteLine(System.Environment.NewLine);
}
答案 25 :(得分:2)
这是一个Haskell版本:
import Data.List
main=putStrLn$unlines$map(uncurry(\n->(++)("On the "++show n++case n of{1->"st";2->"nd";3->"rd";_->"th"}++" day of Christmas, my true love gave to me: ")))$zip[1..]$(\a->(drop 4$head a):tail a)$ map(intercalate", ".reverse)$tail$inits["and a partridge in a pair tree","two turtle doves","three french hens","four calling birds","five gold rings","six geese a-laying","seven swans a-swimming","eight maids a milking","nine ladies dancing","ten lords a-leaping","eleven pipers piping","twelve drummers drumming"]
它有527个字符。由于没有良好的间距,这是相当难以理解的,这是一个更加展开的版本:
import Data.List
main = putStrLn
$ unlines
$ map (uncurry (\n -> (++) ("On the " ++ show n ++ case n of { 1 -> "st"; 2 -> "nd"; 3 -> "rd"; _ -> "th"} ++ " day of Christmas, my true love gave to me: ")))
$ zip [1..]
$ (\a -> (drop 4 $ head a) : tail a)
$ tail
$ map (intercalate ", " . reverse)
(inits ["and a partridge in a pair tree", "two turtle doves", "three french hens", "four calling birds", "five gold rings", "six geese a-laying", "seven swans a-swimming", "eight maids a milking", "nine ladies dancing", "ten lords a-leaping", "eleven pipers piping", "twelve drummers drumming"])
答案 26 :(得分:2)
另一个C#
var l="a partridge in a pear tree.|two turtle doves, and|three french hens,|four calling birds,|five gold rings,|six geese a-laying,|seven swans a-swimming,|eight maids a-milking,|nine ladies dancing,|ten lords a-leaping,|eleven pipers piping,|twelve drummers drumming,".Split('|');
for (int i = 1; i < 13; i++)
Console.Write(
"On the {0}{2} day of christmas\rmy true love gave to me \r{1}\r\r",
i,
string.Join("\r", l.Take(i).Reverse().ToArray()),
(i==1?"st":i==2?"nd":i==3?"rd":"th"));
从以前的答案(嘿,代码重用)中大量借鉴,同时增加额外的节省。
一旦你拿出不必要的空格,481个字符:
var l="a partridge in a pear tree.|two turtle doves, and|three french hens,|four calling birds,|five gold rings,|six geese a-laying,|seven swans a-swimming,|eight maids a-milking,|nine ladies dancing,|ten lords a-leaping,|eleven pipers piping,|twelve drummers drumming,".Split('|');for(int i=1;i<13;i++)Console.Write("On the {0}{2} day of christmas, my true love gave to me \r{1}\r\r",i,String.Join("\r",l.Take(i).Reverse().ToArray()),(i==1?"st":i==2?"nd":i==3?"rd":"th"));
答案 27 :(得分:2)
Objective-C / Cocoa
NSArray *days = [NSArray arrayWithObjects:@"first", @"second", @"third", @"forth",
@"fifth", @"six", @"seventh", @"eigth",
@"ninth", @"tenth", @"eleventh", @"twelth", nil];
NSArray *gifts = [NSArray arrayWithObjects:
@"a partridge in a pear tree.\n\n",
@"two turtle doves, and\n",
@"three french hens,\n",
@"four calling birds,\n",
@"five gold rings,\n",
@"six geese a-laying,\n",
@"seven swans a-swimming,\n",
@"eigth maids a-milking,\n",
@"nine ladies dancing,\n",
@"ten lords a-leaping,\n",
@"eleven pipers piping,\n",
@"twelve drummers drumming,\n",
nil];
NSMutableString *aggregator = [NSMutableString string];
for(int i = 0; i < 12; i++) {
[aggregator insertString:[gifts objectAtIndex:i] atIndex:0];
printf("on the %s day of xmas, my true love gave to me %s", [[days objectAtIndex:i] UTF8String], [aggregator UTF8String]);
}
答案 28 :(得分:2)
一行怎么样!这是在C#。
Xmas(){Console.WriteLine("On the first day of Christmas,\r\nmy true love sent to me\r\nA partridge in a pear tree.\r\n\r\nOn the second day of Christmas,\r\nmy true love sent to me\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the third day of Christmas,\r\nmy true love sent to me\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the fourth day of Christmas,\r\nmy true love sent to me\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the fifth day of Christmas,\r\nmy true love sent to me\r\nFive golden rings,\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the sixth day of Christmas,\r\nmy true love sent to me\r\nSix geese a-laying,\r\nFive golden rings,\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the seventh day of Christmas,\r\nmy true love sent to me\r\nSeven swans a-swimming,\r\nSix geese a-laying,\r\nFive golden rings,\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the eighth day of Christmas,\r\nmy true, love sent to me\r\nEight maids a-milking,\r\nSeven swans a-swimming,\r\nSix geese a-laying,\r\nFive golden rings,\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the ninth day of Christmas,\r\nmy true love sent to me\r\nNine ladies dancing,\r\nEight maids a-milking,\r\nSeven swans a-swimming,\r\nSix geese a-laying,\r\nFive golden rings,\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the tenth day of Christmas,\r\nmy true love sent to me\r\nTen lords a-leaping,\r\nNine ladies dancing,\r\nEight maids a-milking,\r\nSeven swans a-swimming,\r\nSix geese a-laying,\r\nfive golden rings,\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the eleventh day of Christmas,\r\nmy true love sent to me\r\nEleven pipers piping,\r\nTen lords a-leaping,\r\nNine ladies dancing,\r\nEight maids a-milking,\r\nSeven swans a-swimming,\r\nSix geese a-laying\r\nFive golden rings,\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree.\r\n\r\nOn the twelfth day of Christmas,\r\nmy true love sent to me\r\nTwelve drummers drumming,\r\nEleven pipers piping,\r\nTen lords a-leaping,\r\nNine ladies dancing,\r\nEight maids a-milking,\r\nSeven swans a-swimming,\r\nSix geese a-laying,\r\nFive golden rings,\r\nFour calling birds,\r\nThree French hens,\r\nTwo turtle doves,\r\nAnd a partridge in a pear tree!");}
答案 29 :(得分:2)
即使它已经结束了一段时间,我仍然想尝试这些代码高尔夫挑战......
我最好用 MATLAB:383 字符(删除空格和行连续符号后)
a = {'twelve drummers drumming';...
'eleven pipers piping';...
'ten lords a-leaping';...
'nine ladies dancing';...
'eight maids a-milking';...
'seven swans a-swimming';...
'six geese a-laying';...
'five gold rings';...
'four calling birds';...
'three french hens';...
'two turtle doves and';...
'a partridge in a pear tree.';’’};
b = {'st','nd','rd','th'};
for i = 1:12,...
disp(char(['On the ',num2str(i),b{min(i,4)},...
' day of Christmas my true love gave to me'],...
a{13-i:13}));...
end
答案 30 :(得分:2)
(443包括可选换行符)
print """
eJzlk91xxCAMhN9TxRZwSRMp4GpQjGw04ccDunOu+wjcgMkkk8vlxRjQst9q4JygnjFLqQpHN+QZ
r75I1UgV8QYtF0bIV8ZC9tGMyCCsVLSIWxiS2pSpWCnzy9N5P7HylJM7fqRuGXopGhjOtuoJZPID
RuqljPh4E2MunCYPz8mMvmw9Z1P5496tHhOFIGnBm2E38+/kkXkIR9o8B4diQGbz03xVPkb4rBwL
c7X79hzoZlCnX2DmK6ch6iZA3ShV466bxNjJ7yINy6J+JE0XIJK4liZKeN/D3HPIJIlHMrZ6BHLC
1URp6tR/MbiO3VQrt7XSAwamtUM/TDM4jL7cXYFVVi61DR38sbukG4fr0GvZFXDlEmPrU//Z8/+j
/n0CsHRGTA==
""".decode("base64").decode("zlib")
答案 31 :(得分:2)
C#:395个字符(不包括空格......)
for (var x = 0; x++ < 12; )
Console.Write(
"On the {0} day of Christmas,\nMy true love gave to me\n{1}\n\n",
x + (x < 2 ? "st" : x < 3 ? "nd" : x < 4 ? "rd" : "th"),
String.Join(
"\n",
"a partridge in a pear tree.|turtle doves and|french hens|calling birds|gold rings|geese a-laying|swans a-swimming|maids a-milking|ladies dancing|lords a-leaping|pipers piping|drummers drumming"
.Split('|').Take(x).Select((y, i) => (i > 0 ? (i + 1) + " " : "") + y).Reverse().ToArray()
)
);
答案 32 :(得分:2)
这是一个erlang版本(约586个字符):
-module (xmas).
-export ([xmas/0]).
xmas() ->
W = ["twelve drummers drumming, ",
"eleven pipers piping, ",
"ten lords a-leaping, ",
"nine ladies dancing, ",
"eight maids a-milking, ",
"seven swans a-swimming, ",
"six geese a-laying, ",
"five gold rings, ",
"four calling birds, ",
"three french hens, ",
"two turtle doves, and ",
"a partridge in a pear tree."],
io:format(lists:foldl(
fun(X,Acc) -> Acc ++ X ++ "~n" end, "",
["On the " ++ day_str(Q) ++
" day of Christmas, my true love gave me " ++
lists:foldl(
fun(X,Acc)-> Acc++X end,
"", lists:nthtail(12-Q,W)) ||
Q <- lists:seq(1,12)]),[]).
day_str(Q) ->
case Q of
1 -> "1st";
2 -> "2nd";
3 -> "3rd";
N -> erlang:integer_to_list(N,10) ++ "th"
end.
答案 33 :(得分:1)
这不会赢,但这是我的Java版本(可编译和可运行),共576个字符。
class T{enum D{first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,twelfth}public static void main(String[] args){String o="",s="s, ",i="ing, ";String t[]={"a partridge in a pear tree","two turtle doves, and ","three french hen"+s,"four calling bird"+s,"five gold ring"+s,"six geese a-lay"+i,"seven swans a-swimm"+i,"eight maids a-milk"+i,"nine ladies danc"+i,"ten lords a-leap"+i,"eleven pipers pip"+i,"twelve drummers drumm"+i};int j=0;while(j<12)System.out.println("On the "+D.values()[j]+" day of Christmas my true love gave to me "+(o=t[j++]+o));}}
答案 34 :(得分:1)
Console.WriteLine(File.ReadAllText("12daysxmas.txt"));
(最好将字符串常量与代码分开,以便您可以轻松地将它们翻译出来。)
此外,我还要对C#4即将发布的dynamic
关键字表示敬意:
string lyric = "On day {0} of Christmas a dynamic gave to me {1} dynamics.";
int total = 0;
for (int day = 1; day <= 12; day++)
{
total += day;
Console.WriteLine(string.Format(lyric, day, total));
}
Console.WriteLine("I wonder what all these dynamics are? I guess I'll find out at runtime.");
答案 35 :(得分:1)
Java,基于Esko的第二个解决方案:
class C{
public static void main(String[]d){
String s="";
d="first1second1third1fourth1fifth1sixth1seventh1eighth1ninth1tenth1eleventh1twelfth".split("1");
String[]g="a partridge in a pear tree1two turtle doves,\nand 1three french hens,\n1four calling birds,\n1five gold rings,\n1six geese a-laying\n1seven swans a-swimming,\n1eight maids a-milking,\n1nine ladies dancing,\n1ten lords a-leaping,\n1eleven pipers piping,\n1twelve drummers drumming,\n".split("1");
for(int i=0;i<12;)
System.out.println("On the "+d[i]+" day of Christmas my true love gave to me:\n"+
(s=g[i++]+s)+".\n");
}
}
如果我计算正确,那就是579个字符(不包括缩进和换行)。这一个是完全可以运行的,不会在一行上打印每个经文。输出是:
圣诞节的第一天,我的真实 爱给了我:
梨里的鹧.. 树。圣诞节的第二天,我的真实 爱给了我:
两只乌龟鸽子,和 梨树上的鹧。。圣诞节的第三天,我的真实 爱给了我:
三只法国母鸡,
两只乌龟鸽子,和 一只鹧a 梨树。...
编辑:稍微改进版本,基于balabaster用一个字符替换常见字符串的想法:
class C{
public static void main(String[]d){
String s="";
d="first1second1third1fourth1fifth1sixth1seventh1eighth1ninth1tenth1eleventh1twelfth".split("1");
String[]g="a partridge in a pear tree1two turtle doves,\nand 1three french hens,\n1four calling birds,\n1five gold rings,\n1six geese a-lay#seven swans a-swimm#eight maids a-milk#nine ladies danc#ten lords a-leap#eleven pipers pip#twelve drummers drumm#".replace("#","ing,\n1").split("1");
for(int i=0;i<12;)
System.out.println("On the "+d[i]+" day of Christmas my true love gave to me:\n"+
(s=g[i++]+s)+".\n");
}
}
现在已经下降到562个字符(尽管我之前添加了一个逗号,但我之前已经错过了。)
答案 36 :(得分:1)
Ruby:458个字符
(添加+ 4个换行符以保持在80列以下)
n=%w{first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth}
s=''
["a partridge in a pear tree","two turtle doves, and","three french hens",
"four calling birds","five gold rings","six geese a-laying","seven swans a-swimming",
"eight maids a-milking","nine ladies dancing","ten lords a leaping",
"eleven pipers piping","twelve drummers drumming"].map{|p|
puts "On the #{n.pop} day of Christmas my true love gave to me: "+(s=p+', '+s)}
答案 37 :(得分:1)
423个字节,含Lingua :: EN ::数字:
use Lingua::EN::Numbers qw/num2en num2en_ordinal/;
@l=map{($i++?num2en($i):'a')." $_"}split/\n/,<<"";
partridge in a pear tree
turtle doves, and
french hens
calling birds
gold rings
geese a-laying
swans a-swimming
maids a-milking
ladies dancing
lords a-leaping
pipers piping
drummers drumming
print"On the ".num2en_ordinal($_+1)
." day of Christmas, my true love gave to me ",
join(', ',reverse@l[0..$_]),"\n\n"for(0..11);
或者不使用Lingua :: EN :: Numbers:
在perl中480字节@n=qw/first second third forth fifth sixth seventh eigth nineth tenth eleventh twelfth/;
@l=<DATA>;chomp@l;
print"On the $n[$_] day of Christmas, my true love gave to me ",
join(', ',reverse@l[0..$_]),"\n\n"for(0..11);
__DATA__
a partridge in a pear tree
two turtle doves, and
three french hens
four calling birds
five gold rings
six geese a-laying
seven swans a-swimming
eight maids a-milking
nine ladies dancing
ten lords a-leaping
eleven pipers piping
twelve drummers drumming
应该可以进一步减少,因为数字非常重复。
答案 38 :(得分:0)
使用单个缓冲区和指针算法的C / C ++版本。一个包含,一个声明和技术上只有两行代码用于打印:
#include <stdio.h>
char *days =
"twelve drummers drumming, \n"
"eleven pipers piping, \n"
"ten lords a-leaping, \n"
"nine ladies dancing, \n"
"eight maids a-milking, \n"
"seven swans a-swimming, \n"
"six geese a-laying, \n"
"five gold rings, \n"
"four calling birds, \n"
"three french hens, \n"
"two turtle doves, and \n"
"a partridge in a pear tree.\n\n";
void main()
{
for (int i = 1; i <= 12; i++)
printf("On the %d%s day of Christmas, my true love gave to me:\n%s",
i,i == 1 ? "st" : (i == 2 ? "nd" : "th"), days + (12-i)*28);
}
答案 39 :(得分:0)
C ++中非常简单的解决方案:
#include <iostream.h>
char*a[]={"a partridge in a pear tree.\n","two turtle doves, and","three french hens,","four calling birds,","five gold rings,","six geese a-laying,","seven swans a-swimming,","eight maids a-milking,","nine ladies dancing,","ten lords a-leaping,","eleven pipers piping,","twelve drummers drumming,"};
char*b[]={"first","second","third","fourth","fifth","sixth","seventh","eighth","nineth","tenth","eleventh", "twelfth"};
int main(){for(int i=0;i<12;i++){std::cout<<"On the "<<b[i]<<" day of Christmas my true love gave to me, \n";for(int j=i;j>=0;j--)std::cout<<a[j]<<" \n";}}
595个字符,但如果允许某些人使用的“第1,第2,第3”等符号,它可以减少到513个字符。
答案 40 :(得分:0)
LUA 540(没有标签&返回)
t={"twelve drummers drumming,","eleven pipers piping,","ten lords a-leaping,","nine ladies dancing,","eight maids a-milking,","seven swans a-swimming,","six geese a-laying,","five gold rings,","four calling birds,","three french hens,","two turtle doves, and","a partridge in a pear tree."}
f=function(i)
r=i.."th"
if (i==1) then r=i.."st"
elseif (i==2) then r=i.."nd"
elseif (i==3) then r=i.."rd"
end
return r
end
for i=1,12 do
s="On the "..f(i).." day of Christmas my true love gave to me"
for j=13-i,12 do
s=s.." "..t[j]
end
print(s)
end
不幸的是,我们没有字符串到数组函数或通过日期库“第一”的方法。在 http://www.lua.org/cgi-bin/demo
正确运行答案 41 :(得分:0)
没有看到任何用于Java(因其最小语法btw而闻名)所以首先我做了这个
String g;
public void sing() {
g = "";
for(Entry<?,?> e : mm("first", "a partridge in a pear tree",
"second", "two turtle doves, and ",
"third", "three french hens, ",
"fourth", "four calling birds, ",
"fifth", "five gold rings, ",
"sixth", "six geese a-laying ",
"seventh", "seven swans a-swimming, ",
"eigth", "eight maids a-milking, ",
"ninth", "nine ladies dancing, ",
"tenth", "ten lords a-leaping, ",
"eleventh", "eleven pipers piping, ",
"twelfth", "twelve drummers drumming, ").entrySet()) {
System.out.println("On the "+e.getKey()+
" day of Christmas my true love gave to me "+e.getValue()+".");
}
}
public Map<?,?> mm(String... s) {
Map m = new LinkedHashMap();
for(int i=0;i<s.length;i=i+2) {
g = s[i+1] + g;
m.put(s[i], g);
}
return m;
}
并意识到它相当复杂(重达708个字符),所以我选择了一个更简单的解决方案(改编自其他答案,仅为了可读性而更改了行):
public void sing() {
String s = "";
String[]d={"first","second","third","fourth",
"fifth","sixth","seventh","eighth",
"ninth","tenth","eleventh","twelfth"};
String[]g={"a partridge in a pear tree",
"two turtle doves, and ",
"three french hens, ",
"four calling birds, ",
"five gold rings, ",
"six geese a-laying ",
"seven swans a-swimming, ",
"eight maids a-milking, ",
"nine ladies dancing, ",
"ten lords a-leaping, ",
"eleven pipers piping, ",
"twelve drummers drumming, "};
int i=0;
while(i<12)
System.out.println("On the "+d[i]+" day of Christmas my true love gave to me "+
(s=g[i++]+s)+".");
}
这是没有空格的576个字符,如果方法签名切换到正确的main(),那么它是596。
如果我有兴趣制作第三个版本,我也会尝试使用枚举技巧。
答案 42 :(得分:0)
这是一个Javascript版本。压缩后我能做的最好是497个字符。
s = "", k = "ing, ", t = [":first:a:partridge in a pear tree",
":second:two:turtle doves, and ",
"th:ird:ree:french hens, ",
"four:th::calling birds, ",
"fi:fth:ve:gold rings, ",
"six:th::geese a-lay" + k,
"seven:th::swans a-swimm" + k,
"eight:h::maids a-milk" + k,
"nin:th:e:ladies danc" + k,
"ten:th::lords a-leap" + k,
"eleven:th::pipers pip" + k,
"twel:fth:ve:drummers drumm"]
for (i = 0; i < 12; i++)
document.write("On the " + (v = t[i].split(":"))[0] + v[1]
+ " day of Christmas my true love gave to me "
+ (s = v[0] + v[2] + " " + v[3] + s) + "<p>")
s="",k="ing, ",t=[":first:a:partridge in a pear tree",":second:two:turtle doves, and ","th:ird:ree:french hens, ","four:th::calling birds, ","fi:fth:ve:gold rings, ","six:th::geese a-lay"+k,"seven:th::swans a-swimm"+k,"eight:h::maids a-milk"+k,"nin:th:e:ladies danc"+k,"ten:th::lords a-leap"+k,"eleven:th::pipers pip"+k,"twel:fth:ve:drummers drumm"];for(i=0;i<12;i++)document.write("On the "+(v=t[i].split(":"))[0]+v[1]+" day of Christmas my true love gave to me "+(s=v[0]+v[2]+" "+v[3]+s)+"<p>")
答案 43 :(得分:0)
以下是C#
的一些不同方法List<string> lines = new List<string>(new string[]{
"twelve drummers drumming,\n\r"
, "eleven pipers piping,\n\r"
, "ten lords a-leaping,\n\r"
, "nine ladies dancing,\n\r"
, "eigth maids a-milking,\n\r"
, "seven swans a-swimming,\n\r"
, "six geese a-laying,\n\r"
, "five gold rings,\n\r"
, "four calling birds,\n\r"
, "three french hens,\n\r"
, "two turtle doves, and \n\r"
, "a partridge in a pear tree."
});
string[] day = {
"first"
,"second"
,"thrid"
,"forth"
,"fifth"
,"six"
,"seventh"
,"eigth"
,"ninth"
,"tenth"
,"eleventh"
,"twelth"
};
for (int i = 0; i < 12; i++)
Console.WriteLine(string.Format("On the {0} day of christmas, my true love gave to me\n\r{1}", day[i], string.Concat(lines.GetRange(11 - i, i + 1).ToArray())));
不是很好。但它很小。 :)
答案 44 :(得分:0)
偶然发现这个帖子看起来很有趣我无法抗拒 - 这是另一个JavaScript版本 - 它不是最短的但是我喜欢它在循环中使用递归和闭包的方式。
(function(){
var v = [
{d:'first', p:'a partridge in a pear tree'},
{d:'second', p:'two turtle doves, and '},
{d:'third', p:'three french hens, '},
{d:'fourth', p:'four calling birds, '},
{d:'fifth', p:'five gold rings, '},
{d:'sixth', p:'six geese a-laying, '},
{d:'seventh', p:'seven swans a-swimming, '},
{d:'eighth', p:'eight maids a-milking, '},
{d:'ninth', p:'nine ladies dancing, '},
{d:'tenth', p:'ten lords a-leaping, '},
{d:'eleveth', p:'eleven pipers piping, '},
{d:'twelth', p:'twelve drummers drumming, '}
];
for (var i=0;i<12;i++)
{
document.write('On the '+v[i].d+' day of Christmas my true love gave to me ');
(function(i){
document.write(v[i].p);
if (i>0)
{
arguments.callee(--i);
}
})(i);
document.write('.<p>');
}
})();