N : The Twelve Days of Christmas

14Dec13

I recently participated in the Advent Programming Contest 2013 organised by the IEEE Student Branch Klagenfurt (with support from Alpen-Adria-Universität Klagenfurt), wherein – as I’d recently received from Udacity a certificate for Introduction to Programming : Problem Solving with Java (CS046) – I challenged myself to program my solutions to the problems posed by the contest using only Java (I’d used C and C++ when I’d participated in the contest in 2012); I solved eighteen of the problems posed by the contest, achieving a rank of 10th (out of 155 participants).

As submissions of solutions to the contest are now closed, I’m posting here the solutions I submitted to the contest.

Problem (N; easy difficulty):

The Twelve Days of Christmas

“The Twelve Days of Christmas” is a Christmas carol that cumulatively enumerates a series of gifts given on each of the twelve days of Christmas. The lyrics for the first verse are:

On the first day of Christmas, my true love sent to me:
A Partridge in a pear-tree

adding another gift in each verse until the last verse:

On the twelfth day of Christmas, my true love sent to me:
12 Bells ringing
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

Problem

Implement a program that writes out the lyrics for this song, from first to last verse. Between each verse (“On the ..”) there should be a newline. Upon printing all twelve verses, your program shall terminate. Hint: please take care of the correct spelling of English ordinal numbers.

Example

Input

(no input)

Output (only in part)

On the first day of Christmas, my true love sent to me:
A Partridge in a pear-tree

On the second day of Christmas, my true love sent to me:
2 Turtle doves
and a Partridge in a pear-tree

On the third day of Christmas, my true love sent to me:
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

...

On the twelfth day of Christmas, my true love sent to me:
12 Bells ringing
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

Solution (N.java):

public class N
{
  public static void main( String p[] )
  {
    final String numbers[] = { "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" };
    final String gifts[] = { "Partridge in a pear-tree", "Turtle doves", "French hens", "Canary birds", "Gold rings", "Geese a laying", "Swans a swimming", "Ladies dancing", "Lords a leaping", "Ships a sailing", "Ladies spinning", "Bells ringing" };
    for( byte i = 0; i < 12; i ++ )
    {
      System.out.println( "On the " + numbers[ i ] + " day of Christmas, my true love sent to me:" );
      for( byte k = i; k > 0; k -- )
        System.out.println(( k + 1 ) + " " + gifts[ k ] );
      System.out.println((( i == 0 )?"A":"and a" ) + " " + gifts[ 0 ] );
      if( i + 1 < 12 )
        System.out.println();
    }
  }
}

Testing:

On the first day of Christmas, my true love sent to me:
A Partridge in a pear-tree

On the second day of Christmas, my true love sent to me:
2 Turtle doves
and a Partridge in a pear-tree

On the third day of Christmas, my true love sent to me:
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the fourth day of Christmas, my true love sent to me:
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the fifth day of Christmas, my true love sent to me:
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the sixth day of Christmas, my true love sent to me:
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the seventh day of Christmas, my true love sent to me:
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the eighth day of Christmas, my true love sent to me:
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the ninth day of Christmas, my true love sent to me:
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the tenth day of Christmas, my true love sent to me:
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the eleventh day of Christmas, my true love sent to me:
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

On the twelfth day of Christmas, my true love sent to me:
12 Bells ringing
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
Advertisements


No Responses Yet to “N : The Twelve Days of Christmas”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: