Code Golf:打印整个“圣诞节的12天"最少代码行中的歌曲 [英] Code Golf: Print the entire "12 Days of Christmas" song in the fewest lines of code

查看:74
本文介绍了Code Golf:打印整个“圣诞节的12天"最少代码行中的歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打印流行的节日歌曲的所有12节.

由12个 verses 表示歌曲中每行的重复,即

By 12 verses I mean the repetition of each line as is sung in the song, ie

第一节: 圣诞节的第一天,我的真爱给了我 梨树上的part.

Verse One: On the first day of Christmas my true love gave to me a partridge in a pear tree.

第二节 圣诞节的第二天,我的真爱给了我 两只乌龟鸽子 还有一棵梨树上的part.

Verse Two On the second day of Christmas my true love gave to me two turtle doves and a partridge in a pear tree.

...

Verse N: 在圣诞节的第九天,我的真爱给了我 (第N-1节,没有第一行) (在第N节中添加了一行)

Verse N: On the nth day of Christmas my true love gave to me (Verse N-1 without the first line) (line added in verse 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个字符.

Above is 412 characters if you take out the whitespace.

这个:

(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个字符:

is 344 characters if you take out whitespace and ~ quoted newlines in the format string:

(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)))

问题似乎已经解决了,该网站正在逼我接受答案.据我所知,这是最短的.如果我接受自己的回答,我有点担心该网站会做什么-可能会授予我自恋者或自慰者的徽章.

It looks like the question has run its course, and the site is nagging me to accept an answer. As far as I can see, this one is the shortest. I'm a little afraid of what the site will do if I accept my own answer - probably award me a Narcissist or Masturbator badge.

您不能接受自己的答案.很公平.我将其保持打开状态.感谢所有回应.

You can't accept your own answers. Fair enough. I'll leave it open. Thanks to everyone who responded.

这篇关于Code Golf:打印整个“圣诞节的12天"最少代码行中的歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆