ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# bressain's solution

## to Twelve Days in the C# Track

Published at Jul 13 2018 · 9 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Output the lyrics to 'The Twelve Days of Christmas'.

``````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 Gold 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 Gold 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 Gold 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 Gold 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 Gold 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 Gold 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 Gold 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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
``````

## Hints

• Try to capture the structure of the song in your code, where you build up the song by composing its parts.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### TwelveDaysTest.cs

``````// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;

public class TwelveDaysTest
{
[Fact]
public void First_day_a_partridge_in_a_pear_tree()
{
var expected = "On the first day of Christmas my true love gave to me, a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(1));
}

[Fact(Skip = "Remove to run test")]
public void Second_day_two_turtle_doves()
{
var expected = "On the second day of Christmas my true love gave to me, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(2));
}

[Fact(Skip = "Remove to run test")]
public void Third_day_three_french_hens()
{
var expected = "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.";
Assert.Equal(expected, TwelveDays.Recite(3));
}

[Fact(Skip = "Remove to run test")]
public void Fourth_day_four_calling_birds()
{
var expected = "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.";
Assert.Equal(expected, TwelveDays.Recite(4));
}

[Fact(Skip = "Remove to run test")]
public void Fifth_day_five_gold_rings()
{
var expected = "On the fifth day of Christmas my true love gave to me, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(5));
}

[Fact(Skip = "Remove to run test")]
public void Sixth_day_six_geese_a_laying()
{
var expected = "On the sixth day of Christmas my true love gave to me, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(6));
}

[Fact(Skip = "Remove to run test")]
public void Seventh_day_seven_swans_a_swimming()
{
var expected = "On the seventh day of Christmas my true love gave to me, 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.";
Assert.Equal(expected, TwelveDays.Recite(7));
}

[Fact(Skip = "Remove to run test")]
public void Eighth_day_eight_maids_a_milking()
{
var expected = "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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(8));
}

[Fact(Skip = "Remove to run test")]
public void Ninth_day_nine_ladies_dancing()
{
var expected = "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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(9));
}

[Fact(Skip = "Remove to run test")]
public void Tenth_day_ten_lords_a_leaping()
{
var expected = "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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(10));
}

[Fact(Skip = "Remove to run test")]
public void Eleventh_day_eleven_pipers_piping()
{
var expected = "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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(11));
}

[Fact(Skip = "Remove to run test")]
public void Twelfth_day_twelve_drummers_drumming()
{
var expected = "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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(12));
}

[Fact(Skip = "Remove to run test")]
public void Recites_first_three_verses_of_the_song()
{
var expected =
"On the first day of Christmas my true love gave to me, a Partridge in a Pear Tree.\n" +
"On the second day of Christmas my true love gave to me, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"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.";
Assert.Equal(expected, TwelveDays.Recite(1, 3));
}

[Fact(Skip = "Remove to run test")]
public void Recites_three_verses_from_the_middle_of_the_song()
{
var expected =
"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.\n" +
"On the fifth day of Christmas my true love gave to me, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"On the sixth day of Christmas my true love gave to me, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(4, 6));
}

[Fact(Skip = "Remove to run test")]
public void Recites_the_whole_song()
{
var expected =
"On the first day of Christmas my true love gave to me, a Partridge in a Pear Tree.\n" +
"On the second day of Christmas my true love gave to me, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"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.\n" +
"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.\n" +
"On the fifth day of Christmas my true love gave to me, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"On the sixth day of Christmas my true love gave to me, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"On the seventh day of Christmas my true love gave to me, 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.\n" +
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n" +
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.";
Assert.Equal(expected, TwelveDays.Recite(1, 12));
}
}``````
``````ï»¿using System.Collections.Generic;

public class TwelveDaysSong
{
private static readonly string[] SongVerses =
{
"On the first day of Christmas my true love gave to me, a Partridge in a Pear Tree.\n",
"On the second day of Christmas my true love gave to me, two Turtle Doves, and a Partridge in a Pear Tree.\n",
"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.\n",
"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.\n",
"On the fifth day of Christmas my true love gave to me, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
"On the sixth day of Christmas my true love gave to me, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
"On the seventh day of Christmas my true love gave to me, 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.\n",
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
"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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.\n",
};

public string Sing()
{
return Verses(1, 12);
}

public string Verse(int verseNumber)
{
if (verseNumber < 1 || verseNumber > 12) return "";

return SongVerses[verseNumber - 1];
}

public string Verses(int start, int end)
{
var verses = new List<string>();

for (int i = start; i <= end; i++)
verses.Add(Verse(i));

return string.Join("\n", verses) + "\n";
}
}``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

While this works, I don't think hard-coding what each exact verse is was the point of the exercise. I think the point was to attempt to find the various patterns and build up each of the verses via some sort of looping logic.

I agree with @MetalKid. This is terribly wasteful of space. The phrase "day of Chrstmas my true love gave to me" is stored 12 times. This would not easily be extendable to a song with 100 verses. Or if we needed to change the name of the first gift from "a Partridge in a Pear Tree" to "a Kookaburra up a Gum Tree", you would have to change the code in twelve places.

Also, wouldn't it be more efficient to use a StringBuilder to generate the final string in Verses instead of a List with a Join() call?

@jkedgar I know there is a cut off point where +, string.Join, string.Concat, and StringBuilder are best for their situations. I believe StringBuilder is the best after 6 strings get fed in for all of them.

FredrikEdenqvist
commented almost 6 years ago

@MetalKid I think the point was to attempt to find the various patterns and build up each of the verses via some sort of looping logic. Good point didn't think of it that way. Joining strings shouldnt be a problem at this level. But they should have pointed that out tho :/

bressain
Solution Author
commented almost 6 years ago

If you peek at the source for string.Join, it ultimately uses a StringBuilder so I feel confident that string.Join is OK to use here.

As far as writing out the strings vs. building them... yeah, I totally traded flexibility for simplicity here :-P. That's what's fascinating about this exercise, I feel that doing it this way or building the verses are both correct but the evidence of the trade-offs are really pronounced.

Sounds fine, though personally I think you missed out on half the fun. :)

I'm not going to lie about it, I couldn't wait to look at other people's approaches to this problem. That's part of the fun of this website. I found this example disappointing. It's a very brute-force method.

I am happy someone posted this solution. I've seen so much over engineering in production code for things that would have been faster to implement and execute if they had just brute forced it.

I think a lot of us small pond developers imagine our boats cruising on the oceans and so a build a Titanic instead of a row boat.

And, isn't this one of the tenets of TDD? Only implement what the tests require in the simplest way. When new more elaborate requirements come along we refactor.

In my opinion, TDD is pretty dangerous. Unless you get those tests exactly right, which is almost impossible... You could end up like this:

http://geek-and-poke.com/geekandpoke/2013/7/28/tdd

### What can you learn from this solution?

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?