The Twelve Days of Christmas

From Rosetta Code
Revision as of 22:11, 19 December 2013 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: added a comment to the REXX section header. -- ~~~~)
The Twelve Days of Christmas is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Write a program that outputs the lyrics of the Christmas carol The Twelve Days of Christmas. The lyrics can be found here. (You must reproduce the words, but formatting and punctuation are left to your discretion.)

ActionScript

This program outputs the lyrics to a TextField object. The text field can be scrolled using the mouse wheel (Windows only) or by using the up/down arrow keys on the keyboard.

Works with: Flash Player version 10
Works with: AIR version 1.5

(Although the code can work in Flash Player 9 by replacing the Vectors with Arrays)

<lang ActionScript>package {

   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.KeyboardEvent;
   import flash.events.MouseEvent;
   import flash.text.TextField;
   import flash.text.TextFieldAutoSize;
   import flash.text.TextFormat;
   import flash.ui.Keyboard;
   
   public class TwelveDaysOfChristmas extends Sprite {
       
       private var _textArea:TextField = new TextField();
       
       public function TwelveDaysOfChristmas() {
           if ( stage ) _init();
           else addEventListener(Event.ADDED_TO_STAGE, _init);
       }
       
       private function _init(e:Event = null):void {
           
           removeEventListener(Event.ADDED_TO_STAGE, _init);
           
           _textArea = new TextField();
           _textArea.x = 10;
           _textArea.y = 10;
           _textArea.autoSize = TextFieldAutoSize.LEFT;
           _textArea.wordWrap = true;
           _textArea.width = stage.stageWidth - 20;
           _textArea.height = stage.stageHeight - 10;
           _textArea.multiline = true;
           
           var format:TextFormat = new TextFormat();
           format.size = 14;
           _textArea.defaultTextFormat = format;
           
           var verses:Vector.<String> = new Vector.<String>(12, true);
           var lines:Vector.<String>;
           
           var days:Vector.<String> = new Vector.<String>(12, true);
           var gifts:Vector.<String> = new Vector.<String>(12, true);
           
           days[0] = 'first';
           days[1] = 'second';
           days[2] = 'third';
           days[3] = 'fourth';
           days[4] = 'fifth';
           days[5] = 'sixth';
           days[6] = 'seventh';
           days[7] = 'eighth';
           days[8] = 'ninth';
           days[9] = 'tenth';
           days[10] = 'eleventh';
           days[11] = 'twelfth';
           
           gifts[0] = 'A partridge in a pear tree';
           gifts[1] = 'Two turtle doves';
           gifts[2] = 'Three french hens ';
           gifts[3] = 'Four calling birds';
           gifts[4] = 'Five golden rings';
           gifts[5] = 'Six geese a-laying';
           gifts[6] = 'Seven swans a-swimming';
           gifts[7] = 'Eight maids a-milking';
           gifts[8] = 'Nine ladies dancing';
           gifts[9] = 'Ten lords a-leaping';
           gifts[10] = 'Eleven pipers piping';
           gifts[11] = 'Twelve drummers drumming';
           
           var i:uint, j:uint, k:uint, line:String, gift:String;
           
           for ( i = 0; i < 12; i++ ) {
               
               lines = new Vector.<String>(i + 2, true);
               lines[0] = "On the " + days[i] + " day of Christmas, my true love gave to me";
               
               j = i + 1;
               k = 0;
               while ( j-- > 0 ) {
                   gift = gifts[j];
                   lines[++k] = gift;
               }
               
               verses[i] = lines.join('\n');
               
               if ( i == 0 )
                   gifts[0] = 'And a partridge in a pear tree';
               
           }
           
           var song:String = verses.join('\n\n');
           _textArea.text = song;
           addChild(_textArea);
           
           _textArea.addEventListener(MouseEvent.MOUSE_WHEEL, _onMouseWheel);
           stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
           
       }
       
       private function _onKeyDown(e:KeyboardEvent):void {
           if ( e.keyCode == Keyboard.DOWN )
               _textArea.y -= 40;
           else if ( e.keyCode == Keyboard.UP )
               _textArea.y += 40;
       }
       
       private function _onMouseWheel(e:MouseEvent):void {
           _textArea.y += 20 * e.delta;
       }
       
   }
   

}</lang>

C#

<lang csharp>using System;

public class TwelveDaysOfChristmas {

   public static void Main() {
       string[] days = new string[12] {
           "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
           "tenth", "eleventh", "twelfth",
       };
       string[] gifts = new string[12] {
           "A partridge in a pear tree",
           "Two turtle doves",
           "Three french hens ",
           "Four calling birds",
           "Five golden 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 ( int i = 0; i < 12; i++ ) {
           Console.WriteLine("On the " + days[i] + " day of Christmas, my true love gave to me");
           int j = i + 1;
           while ( j-- > 0 ) {
               Console.WriteLine(gifts[j]);
           }
           Console.WriteLine();
           if ( i == 0 )
               gifts[0] = "And a partridge in a pear tree";
       }
   }

}</lang>

J

<lang j>require 'strings' NB. not necessary for versions > j6

days=: ;:'first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth'

gifts=: <;.2 ] 0 : 0 And a partridge in a pear tree. Two turtle doves, Three french hens, Four calling birds, Five golden 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, )

firstline=: 'On the ' , ,&(' day of Christmas, my true love gave to me',LF)

chgFirstVerse=: rplc&('nd a';)&.>@{. , }.

makeVerses=: [: chgFirstVerse (firstline&.> days) ,&.> [: <@;@|.\ gifts"_

singCarol=: LF joinstring makeVerses</lang>

Perl 6

<lang perl6>my @days = <first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth>;

my @gifts = lines q:to//;

 And a partridge in a pear tree.
 Two turtle doves,
 Three french hens,
 Four calling birds,
 Five golden 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,

sub nth($n) { say "On the @days[$n] day of Christmas, my true love gave to me:" }

nth(0); say @gifts[0].subst('nd a',);

for 1 ... 11 -> $d {

   say ;
   nth($d);
   say @gifts[$_] for $d ... 0;

}</lang>

REXX

This version:

  • capitalizes the first word of each verse line
  • adds a comma after each item in the list
  • adds a period at the end of each verse
  • does NOT capitalize the word Twelfth
  • capitalizes the French word (French hen)
  • moved the and (for the last gift) to the last line
  • added indentation to make verses look like a poem

<lang rexx>/*REXX program displays the verses of song "The 12 days of Christmas". */ Nth = 'first second third four fif six seven eigh nin ten eleven twelf' pad = left(,20)

                    d.1  = 'a partridge in a pear-tree'
                    d.2  = 'two turtle doves'
                    d.3  = 'three French hens'
                    d.4  = 'four calling birds'
                    d.5  = 'five golden rings'
                    d.6  = 'six geese a-laying'
                    d.7  = 'seven swans a-swimming'
                    d.8  = 'eight maids a-milking'
                    d.9  = 'nine ladies dancing'
                    d.10 = 'ten lords a-leaping'
                    d.11 = 'eleven pipers piping'
                    d.12 = 'twelve drummers drumming'
 do day=1  for 12
 say pad 'On the' word(Nth,day)||word('th',1+(day<4))  'Day of Christmas'
 say pad 'My True Love gave to me:'
      do j=day  to 1  by -1;   and=word('and', 1+(j\==1 | day==1))
      p=space(and  d.j || substr(',.', 1+(j==1), 1))
      say pad translate(left(p,1))||substr(p,2)
      end   /*j*/
 say
 end        /*day*/
                                      /*stick a fork in it, we're done.*/</lang>

output

                     On the first Day of Christmas
                     My true love gave to me:
                     A partridge in a pear-tree.

                     On the second Day of Christmas
                     My true love gave to me:
                     Two turtle doves,
                     And a partridge in a pear-tree.

                     On the third Day of Christmas
                     My true love gave to me:
                     Three French hens,
                     Two turtle doves,
                     And a partridge in a pear-tree.

                     On the fourth Day of Christmas
                     My true love gave to me:
                     Four calling birds,
                     Three French hens,
                     Two turtle doves,
                     And a partridge in a pear-tree.

                     On the fifth Day of Christmas
                     My true love gave to me:
                     Five golden rings,
                     Four calling birds,
                     Three French hens,
                     Two turtle doves,
                     And a partridge in a pear-tree.

                     On the sixth Day of Christmas
                     My true love gave to me:
                     Six geese a-laying,
                     Five golden rings,
                     Four calling birds,
                     Three French hens,
                     Two turtle doves,
                     And a partridge in a pear-tree.

                     On the seventh Day of Christmas
                     My true love gave to me:
                     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.

                     On the eighth Day of Christmas
                     My true love gave to me:
                     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.

                     On the ninth Day of Christmas
                     My true love gave to me:
                     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.

                     On the tenth Day of Christmas
                     My true love gave to me:
                     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.

                     On the eleventh Day of Christmas
                     My true love gave to me:
                     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.

                     On the twelfth Day of Christmas
                     My true love gave to me:
                     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.