Avatar of Astrimedes

Astrimedes's solution

to Twelve Days in the C# Track

Published at Jul 13 2018 · 1 comment
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.

Source

Wikipedia http://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song)

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;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace exercism.twelve_days
{
	/// <summary>
	/// Description of TwelveDaysSong.
	/// </summary>
	public class TwelveDaysSong
	{
		static readonly string verseBegin = "On the @DAYNUM#@ day of Christmas my true love gave to me,";
		static readonly string verseGift = "@GIFTNUM#@ @GIFTNAME#@";
		
		private Translator translator;
		
		private StringBuilder allVersesBuilder = new StringBuilder();
		private StringBuilder verseBuilder = new StringBuilder();
		
		public TwelveDaysSong()
		{
			// define token
			translator = new Translator("@");
			
			// add gift phrase replacements
			string[] numbers = new string[]	{
				"Partridge in a Pear Tree",
				"Turtle Doves",
				"French Hens",
				"Calling Birds",
				"Gold Rings",
				"Geese-a-Laying",
				"Swans-a-Swimming",
				"Maids-a-Milking",
				"Ladies Dancing",
				"Lords-a-Leaping",
				"Pipers Piping",
				"Drummers Drumming"
			};
			for (int i = 0; i < 12; i++) {
				translator.AddReplacements("GIFTNAME"+(i+1).ToString(), numbers[i]);
			}
			
			// add number of gift replacements
			numbers = new string[] {
				"and a", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"
			};
			for (int i = 0; i < 12; i++) {
				translator.AddReplacements("GIFTNUM"+(i+1).ToString(), numbers[i]);
			}
			
			// add number of days replacements
			numbers = new string[] {
				"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"
			};
			for (int i = 0; i < 12; i++) {
				translator.AddReplacements("DAYNUM"+(i+1).ToString(), numbers[i]);
			}
		}
		
		public string Verse(int dayOfXmas)
		{
			// replaces number placeholders with appropriate number
			string verseOpen = translator.Translate(Regex.Replace(verseBegin, @"\#", dayOfXmas.ToString()));
			verseBuilder.Clear();
			
			// assemble the entire gift string
			for(int i = dayOfXmas; i > 0; i--) {
				verseBuilder.Append(", ").Append(translator.Translate(Regex.Replace(verseGift, @"\#", i.ToString())));
			}
			
			// remove extra comma and unnecessary 'and' before first element, then return complete verse
			return verseOpen + Regex.Replace(verseBuilder.ToString(), @"^,\s*(and)*\s*", " ") + ".\n";
		}
		
		public string Verses(int day1, int day2)
		{
			allVersesBuilder.Clear();
			
			int first = Math.Min(day1, day2);
			int last = first == day1 ? day2 : day1;
			
			for (int i = first; i <= last; i++) {
				allVersesBuilder.Append("\n").Append(Verse(i));
			}
			
			return allVersesBuilder.Append("\n").Remove(0,1).ToString();
		}
		
		public string Sing()
		{
			return Verses(1,12);
		}
	}
	
	public class Translator
	{
		private string MATCH_TOKEN;
		private string TOKEN;
		
		private Dictionary<string,string> replacements = new Dictionary<string,string>();
		
		private StringBuilder builder = new StringBuilder();
		
		public Translator(string startToken)
		{
			TOKEN = startToken;
			MATCH_TOKEN = TOKEN + ".*?" + TOKEN;
		}
		
		public void AddReplacements(params string[] keysThenValues)
		{
			if (keysThenValues.Length % 2 != 0)
				throw new ArgumentException("You must provice an even number of keys, then values!", "keysThenValues");
			
			string key;
			string val;
			
			for (int i = 1; i < keysThenValues.Length; i++) {
				
				key = keysThenValues[i-1];
				val = keysThenValues[i];
				
				if (key.Contains(TOKEN) || val.Contains(TOKEN))
					throw new ArgumentException("The keys or replacement values can't contain the token! (Key=" +
					                            key + "; Value=" + val + ")", "keysThenValues");
				
				replacements.Add(TOKEN + key + TOKEN, val);
			}
		}
		
		public string Translate(string input)
		{
			builder.Clear();
			builder.Append(input);
			
			Match match = Regex.Match(input, MATCH_TOKEN);
			while(match.Success)
			{
				builder.Replace(match.Value, replacements[match.Value]);
				
				match = match.NextMatch();
			}
			
			return builder.ToString();
		}
	}
}

Community comments

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

I've seen several people use custom token replacement methods (an honestly I have one in my toolkit) and I curious as to why not use string.Format?

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?