Avatar of artemkorsakov

artemkorsakov's solution

to House in the C# Track

Published at Feb 25 2019 · 0 comments
Instructions
Test suite
Solution

Recite the nursery rhyme 'This is the House that Jack Built'.

[The] process of placing a phrase of clause within another phrase of clause is called embedding. It is through the processes of recursion and embedding that we are able to take a finite number of forms (words and phrases) and construct an infinite number of expressions. Furthermore, embedding also allows us to construct an infinitely long structure, in theory anyway.

The nursery rhyme reads as follows:

This is the house that Jack built.

This is the malt
that lay in the house that Jack built.

This is the rat
that ate the malt
that lay in the house that Jack built.

This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

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 House.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

British nursery rhyme http://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built

Submitting Incomplete Solutions

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

HouseTest.cs

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

using Xunit;

public class HouseTest
{
    [Fact]
    public void Verse_one_the_house_that_jack_built()
    {
        var expected = "This is the house that Jack built.";
        Assert.Equal(expected, House.Recite(1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_two_the_malt_that_lay()
    {
        var expected = "This is the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_three_the_rat_that_ate()
    {
        var expected = "This is the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(3));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_four_the_cat_that_killed()
    {
        var expected = "This is the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(4));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_five_the_dog_that_worried()
    {
        var expected = "This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(5));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_six_the_cow_with_the_crumpled_horn()
    {
        var expected = "This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(6));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_seven_the_maiden_all_forlorn()
    {
        var expected = "This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(7));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_eight_the_man_all_tattered_and_torn()
    {
        var expected = "This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(8));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_nine_the_priest_all_shaven_and_shorn()
    {
        var expected = "This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(9));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_10_the_rooster_that_crowed_in_the_morn()
    {
        var expected = "This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(10));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_11_the_farmer_sowing_his_corn()
    {
        var expected = "This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(11));
    }

    [Fact(Skip = "Remove to run test")]
    public void Verse_12_the_horse_and_the_hound_and_the_horn()
    {
        var expected = "This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(12));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_verses()
    {
        var expected = 
            "This is the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(4, 8));
    }

    [Fact(Skip = "Remove to run test")]
    public void Full_rhyme()
    {
        var expected = 
            "This is the house that Jack built.\n" +
            "This is the malt that lay in the house that Jack built.\n" +
            "This is the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n" +
            "This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.";
        Assert.Equal(expected, House.Recite(1, 12));
    }
}
using System;
using System.Linq;

public static class House
{
    private static readonly string[] FirstRow = { "This is the house that Jack built.", "This is the malt", "This is the rat",
        "This is the cat", "This is the dog", "This is the cow with the crumpled horn",
        "This is the maiden all forlorn", "This is the man all tattered and torn", "This is the priest all shaven and shorn",
        "This is the rooster that crowed in the morn", "This is the farmer sowing his corn", "This is the horse and the hound and the horn" };
    private static readonly string[] NextRows = { " that belonged to the farmer sowing his corn",
        " that kept the rooster that crowed in the morn",
        " that woke the priest all shaven and shorn",
        " that married the man all tattered and torn",
        " that kissed the maiden all forlorn",
        " that milked the cow with the crumpled horn",
        " that tossed the dog",
        " that worried the cat",
        " that killed the rat",
        " that ate the malt",
        " that lay in the house that Jack built." };

    public static string Recite(int verseNumber)
    {
        if (verseNumber < 1 || verseNumber > 12)
        {
            throw new ArgumentException("verseNumber must be between 1 and 12");
        }
        var result = FirstRow[verseNumber - 1];
        for (var i = verseNumber - 1; i > 0; i--)
        {
            result += NextRows[NextRows.Length - i];
        }

        return result;
    }

    public static string Recite(int startVerse, int endVerse)
    {
        return Enumerable.Range(startVerse, endVerse - startVerse + 1).Select(i => Recite(i))
            .Aggregate("", (current, str) => current + str + "\n").TrimEnd('\n');
    }
}

Community comments

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

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?