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

# freddie2025's solution

## to Twelve Days in the C# Track

Published at Jan 28 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit TwelveDays.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

### TwelveDaysTest.cs

``````// This file was auto-generated based on version 1.2.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")]
{
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;
using System.Text;
using System.Linq;
using System.Collections.Generic;

public static class TwelveDays
{
static SortedDictionary<int, Tuple<string, string>> Verses = new SortedDictionary<int, Tuple<string, string>>()
{
{1, new Tuple<string, string>("first", "a Partridge in a Pear Tree")},
{2, new Tuple<string, string>("second", "two Turtle Doves, and ")},
{3, new Tuple<string, string>("third", "three French Hens, ")},
{4, new Tuple<string, string>("fourth", "four Calling Birds, ")},
{5, new Tuple<string, string>("fifth", "five Gold Rings, ")},
{6, new Tuple<string, string>("sixth", "six Geese-a-Laying, ")},
{7, new Tuple<string, string>("seventh", "seven Swans-a-Swimming, ")},
{8, new Tuple<string, string>("eighth", "eight Maids-a-Milking, ")},
{9, new Tuple<string, string>("ninth", "nine Ladies Dancing, ")},
{10, new Tuple<string, string>("tenth", "ten Lords-a-Leaping, ")},
{11, new Tuple<string, string>("eleventh", "eleven Pipers Piping, ")},
{12, new Tuple<string, string>("twelfth", "twelve Drummers Drumming, ")}
};

public static string Recite(int verseNumber)
{
return \$"On the {Verses[verseNumber].Item1} day of Christmas my true love gave to me: {Verses.Take(verseNumber).Reverse().Aggregate(new StringBuilder(), (x, y) => x.Append(y.Value.Item2))}.";
}

public static string Recite(int startVerse, int endVerse)
{
var sb = new StringBuilder();
for(int i = startVerse; i <= endVerse; i++)
{
sb.Append(Recite(i) + '\n');
}
return sb.ToString().Trim('\n');
}
}``````