Avatar of artemkorsakov

artemkorsakov's solution

to Food Chain in the C# Track

Published at Apr 21 2019 · 0 comments
Instructions
Test suite
Solution

Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.

While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.

This is a cumulative song of unknown origin.

This is one of many common variants.

I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a spider.
It wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a bird.
How absurd to swallow a bird!
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cat.
Imagine that, to swallow a cat!
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a dog.
What a hog, to swallow a dog!
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a goat.
Just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cow.
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a horse.
She's dead, of course!

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

Wikipedia http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly

FoodChainTest.cs

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

using Xunit;

public class FoodChainTest
{
    [Fact]
    public void Fly()
    {
        var expected = 
            "I know an old lady who swallowed a fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.";
        Assert.Equal(expected, FoodChain.Recite(1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Spider()
    {
        var expected = 
            "I know an old lady who swallowed a spider.\n" +
            "It wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.";
        Assert.Equal(expected, FoodChain.Recite(2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Bird()
    {
        var expected = 
            "I know an old lady who swallowed a bird.\n" +
            "How absurd to swallow a bird!\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.";
        Assert.Equal(expected, FoodChain.Recite(3));
    }

    [Fact(Skip = "Remove to run test")]
    public void Cat()
    {
        var expected = 
            "I know an old lady who swallowed a cat.\n" +
            "Imagine that, to swallow a cat!\n" +
            "She swallowed the cat to catch the bird.\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.";
        Assert.Equal(expected, FoodChain.Recite(4));
    }

    [Fact(Skip = "Remove to run test")]
    public void Dog()
    {
        var expected = 
            "I know an old lady who swallowed a dog.\n" +
            "What a hog, to swallow a dog!\n" +
            "She swallowed the dog to catch the cat.\n" +
            "She swallowed the cat to catch the bird.\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.";
        Assert.Equal(expected, FoodChain.Recite(5));
    }

    [Fact(Skip = "Remove to run test")]
    public void Goat()
    {
        var expected = 
            "I know an old lady who swallowed a goat.\n" +
            "Just opened her throat and swallowed a goat!\n" +
            "She swallowed the goat to catch the dog.\n" +
            "She swallowed the dog to catch the cat.\n" +
            "She swallowed the cat to catch the bird.\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.";
        Assert.Equal(expected, FoodChain.Recite(6));
    }

    [Fact(Skip = "Remove to run test")]
    public void Cow()
    {
        var expected = 
            "I know an old lady who swallowed a cow.\n" +
            "I don't know how she swallowed a cow!\n" +
            "She swallowed the cow to catch the goat.\n" +
            "She swallowed the goat to catch the dog.\n" +
            "She swallowed the dog to catch the cat.\n" +
            "She swallowed the cat to catch the bird.\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.";
        Assert.Equal(expected, FoodChain.Recite(7));
    }

    [Fact(Skip = "Remove to run test")]
    public void Horse()
    {
        var expected = 
            "I know an old lady who swallowed a horse.\n" +
            "She's dead, of course!";
        Assert.Equal(expected, FoodChain.Recite(8));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_verses()
    {
        var expected = 
            "I know an old lady who swallowed a fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a spider.\n" +
            "It wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a bird.\n" +
            "How absurd to swallow a bird!\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.";
        Assert.Equal(expected, FoodChain.Recite(1, 3));
    }

    [Fact(Skip = "Remove to run test")]
    public void Full_song()
    {
        var expected = 
            "I know an old lady who swallowed a fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a spider.\n" +
            "It wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a bird.\n" +
            "How absurd to swallow a bird!\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a cat.\n" +
            "Imagine that, to swallow a cat!\n" +
            "She swallowed the cat to catch the bird.\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a dog.\n" +
            "What a hog, to swallow a dog!\n" +
            "She swallowed the dog to catch the cat.\n" +
            "She swallowed the cat to catch the bird.\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a goat.\n" +
            "Just opened her throat and swallowed a goat!\n" +
            "She swallowed the goat to catch the dog.\n" +
            "She swallowed the dog to catch the cat.\n" +
            "She swallowed the cat to catch the bird.\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a cow.\n" +
            "I don't know how she swallowed a cow!\n" +
            "She swallowed the cow to catch the goat.\n" +
            "She swallowed the goat to catch the dog.\n" +
            "She swallowed the dog to catch the cat.\n" +
            "She swallowed the cat to catch the bird.\n" +
            "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
            "She swallowed the spider to catch the fly.\n" +
            "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
            "\n" +
            "I know an old lady who swallowed a horse.\n" +
            "She's dead, of course!";
        Assert.Equal(expected, FoodChain.Recite(1, 8));
    }
}
public static class FoodChain
{
    private static readonly string[] firstLines = new string[]{
        "I know an old lady who swallowed a fly.\n",
        "I know an old lady who swallowed a spider.\nIt wriggled and jiggled and tickled inside her.\n",
        "I know an old lady who swallowed a bird.\nHow absurd to swallow a bird!\n",
        "I know an old lady who swallowed a cat.\nImagine that, to swallow a cat!\n",
        "I know an old lady who swallowed a dog.\nWhat a hog, to swallow a dog!\n",
        "I know an old lady who swallowed a goat.\nJust opened her throat and swallowed a goat!\n",
        "I know an old lady who swallowed a cow.\nI don't know how she swallowed a cow!\n",
        "I know an old lady who swallowed a horse.\nShe's dead, of course!"};
    private static readonly string[] lastLines = new string[]{
        "I don't know why she swallowed the fly. Perhaps she'll die.",
        "She swallowed the spider to catch the fly.\n",
        "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n",
        "She swallowed the cat to catch the bird.\n",
        "She swallowed the dog to catch the cat.\n",
        "She swallowed the goat to catch the dog.\n",
        "She swallowed the cow to catch the goat.\n"};

    public static string Recite(int verseNumber)
    {
        if (verseNumber < 1 || verseNumber > 8)
        {
            return "";
        }
        if (verseNumber == 8)
        {
            return firstLines[7];
        }
        var result = firstLines[verseNumber - 1];
        for (int i = verseNumber - 1; i >= 0; i--)
        {
            result += lastLines[i];
        }
        return result;
    }

    public static string Recite(int startVerse, int endVerse)
    {
        if (startVerse > endVerse)
        {
            return "";
        }
        var result = Recite(startVerse);
        for (int i = startVerse + 1; i <= endVerse; i++)
        {
            result += "\n\n" + Recite(i);
        }
        return result;
    }
}

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?