人気のホリデー ソングの全 12 節を印刷します。
12の詩とは、歌の中で歌われている各行の繰り返しを意味します。
第 1 節: クリスマスの初日に、私の本当の恋人がナシの木にとまったヤマウズラをくれました。
2 節 クリスマスの 2 日目に、私の本当の恋人が 2 羽のキジバトとナシの木にとまったヤマウズラをくれました。
...
N 節: クリスマスの n 日目に、私の本当の愛は私にくれました (N-1 節、最初の行なし) (N 節に追加された行)
人気のホリデー ソングの全 12 節を印刷します。
12の詩とは、歌の中で歌われている各行の繰り返しを意味します。
第 1 節: クリスマスの初日に、私の本当の恋人がナシの木にとまったヤマウズラをくれました。
2 節 クリスマスの 2 日目に、私の本当の恋人が 2 羽のキジバトとナシの木にとまったヤマウズラをくれました。
...
N 節: クリスマスの n 日目に、私の本当の愛は私にくれました (N-1 節、最初の行なし) (N 節に追加された行)
一般的な 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)))
編集:
質問は一通り終わったようで、サイトは私に回答を受け入れるようせつめいています。私が見る限り、これが一番短いです。私が自分の回答を受け入れると、サイトがどうなるか少し心配です。おそらく、ナルシストまたはオナホールのバッジを授与してくれるでしょう。
自分の答えを受け入れることはできません。けっこうだ。開けておきます。回答してくれたすべての人に感謝します。
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))
12日目の出力:
クリスマスの12番目のバグについて、私のマネージャーは私に言った それが機能だと彼らに伝えてください サポートされていないと言う ドキュメントを変更する ハードウェアのせい それを回避する方法を見つける アップグレードが必要だと言う ソフトウェアを再インストールします ダンプを要求する デバッガーで実行 それを再現してみてください 彼らがそれをどのようにしたかを彼らに尋ね、 彼らが再びそれを行うことができるかどうかを確認してください。
私のものではありませんが、興味深いです...
これは、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);}
「一見理解できませんが、コンパイルして実行すると、12日間のクリスマスの12節が生成される、合法的なCプログラムです。コードにインライン化されたエンコード形式で詩に必要なすべての文字列が含まれています。コード必要なものを表示する12日間を繰り返します。」
ゴルフには勝てませんが、約830バイトです。
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();
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");
}
}
}
C++ では、
std::cout << boost::12_days_of_christmas;
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);
$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;}
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
オブジェクトへの Linq、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));
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文字)
もう少し素敵で表現力豊かにしたかったのですが、これはゴルフの挑戦です。
このスタイルは、適切なコーディング標準にいくらか違反しています。しかし、それはあなたにとってゴルフです。
これは、卑劣なトリックが少ない、コンパクトではないバージョンです。
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";
}
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 文字
シーズン到来!(そして、LilyPond が主に音楽のタイプセットに使用されていることを考えると、かなり適切です...このコードを簡単に適用して、歌詞だけでなく曲の楽譜を作成することもできます。)
フィザーのソリューションから適応。
#(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 の出力と正確に一致しますが、3 バイト短くなっています。
#(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))
ルビー。これはかなり簡潔だと思います(ハードワークはすべて1行にあります):
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(", ")
}
perl -MTemplate -e 'Template->new()->process("12dayxmas.tt")'
[% リスト=[ {日=>'最初の'、アイテム=>'ナシの木のヤマウズラ。'}、 {日=>'秒'、アイテム=>'2羽のカメハト、および'}、 {日=>'3番目'、アイテム=>'3羽のフランスの鶏'}、 {日=>'4番目'、アイテム=>'4羽の鳴き鳥、'}、 {日=>'5番目'、アイテム=>'5つの金の指輪'}、 {日=>'6番目'、アイテム=>'6匹のガチョウの産卵、'}、 {日=>'7番目'、アイテム=>'7羽の白鳥a-水泳、'}、 {日=>'8番目'、アイテム=>'8人のメイドa-搾乳、'}、 {日=>'nineth'、アイテム=>'9人の女性が踊る'}、 {日=>'10番目'、アイテム=>'10人の領主が飛び跳ねる'}、 {日=>'11番目'、アイテム=>'11個のパイパー配管、'}、 {日=>'12番目'、アイテム=>'12人のドラマーがドラミング、'} ]; -%] [% FOREACHリスト; present = item _ present; -%] クリスマスの[%day%]日に、私の本当の愛が私に与えてくれました[%present%] [% 終わり %]
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");
}
h
コマンドh
が「Hello、World!」を出力する私の作成した言語で。ちょっと待って、それは私たちが話していることではないということですか?
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)))
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文字。数値拡張を正しく行うために、いくつかの文字を追加します。ただし、おそらくかなり改善できるでしょう。
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);
}
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年の幸せな休日と素晴らしいプログラミング。
26バイト!..
URLが示すように、それは考慮されるかもしれません。
http://github.com/dbr/so_scripts/tree/master/golf_tdoc/cheating.bash
ルビー
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 バイト。
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];
}
これが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.
別の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"));
Objective-C/ココア
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]);
}
1行でなんと!これは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!");}
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()
)
);
(オプションの改行を含む 443)
print """
eJzlk91xxCAMhN9TxRZwSRMp4GpQjGw04ccDunOu+wjcgMkkk8vlxRjQst9q4JygnjFLqQpHN+QZ
r75I1UgV8QYtF0bIV8ZC9tGMyCCsVLSIWxiS2pSpWCnzy9N5P7HylJM7fqRuGXopGhjOtuoJZPID
RuqljPh4E2MunCYPz8mMvmw9Z1P5496tHhOFIGnBm2E38+/kkXkIR9o8B4diQGbz03xVPkb4rBwL
c7X79hzoZlCnX2DmK6ch6iZA3ShV466bxNjJ7yINy6J+JE0XIJK4liZKeN/D3HPIJIlHMrZ6BHLC
1URp6tR/MbiO3VQrt7XSAwamtUM/TDM4jL7cXYFVVi61DR38sbukG4fr0GvZFXDlEmPrU//Z8/+j
/n0CsHRGTA==
""".decode("base64").decode("zlib")
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"])
Console.WriteLine(File.ReadAllText("12daysxmas.txt"));
(文字列定数をコードから分離して、簡単に変換できるようにすることをお勧めします。)
dynamic
また、C#4の次のキーワードに関する私の賛辞もあります。
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.");
これでは勝てませんが、私の 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));}}
Lingua::EN::Number で 423 バイト:
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
数値は非常に反復的であるため、さらに削減できるはずです。
Ruby: 458 文字
(+ 80 列未満にするために 4 つの改行を追加)
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)}
Esko の 2 番目のソリューションに基づく Java:
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 文字です (インデントと改行は数えません)。これは完全に実行可能で、各詩を 1 行に出力しません。出力は次のとおりです。
クリスマスの初日、私の本当の愛は私にくれました:
ナシの木のヤマウズラ。
クリスマスの 2 日目に、私の本当の恋人が私にくれまし
た。クリスマスの 3 日目に、私の本当の恋人がくれました
。3 羽のフランス鶏、
2 羽
のキジバト、ナシの木のヤマウズラです。...
編集:一般的な文字列を1文字に置き換えるバラバスターのアイデアに基づいて、わずかに改善されたバージョン:
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 文字になりました (以前は見逃していたコンマを追加しましたが)。
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文字に減らすことができます。
Java(最小限の構文で知られていない)のものが見られなかったので、最初にこれを作成しました
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 文字になります。
3 番目のバージョンを作成することに興味があれば、enum トリックも試してみたいと思います。
ここに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())));
必ずしもいいとは言えません。しかし小さい。:)
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
たまたまこのスレッドにたどり着きましたが、とても楽しそうに見えて我慢できませんでした - これは別の 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>');
}
})();
単一のバッファーとポインター演算を使用する AC/C++ バージョン。単一のインクルード、1 つの宣言、および印刷を行うためのメインのコードは技術的には 2 行だけです。
#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);
}