Avatar of artemkorsakov

artemkorsakov's solution

to Food Chain in the Java Track

Published at Feb 05 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!

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

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

Submitting Incomplete Solutions

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

FoodChainTest.java

import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class FoodChainTest {
    private FoodChain foodChain;

    @Before
    public void setup() {
        foodChain = new FoodChain();
    }

    @Test
    public void fly() {
        int verse = 1;
        String expected = "I know an old lady who swallowed a fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void spider() {
        int verse = 2;
        String 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.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void bird() {
        int verse = 3;
        String 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.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void cat() {
        int verse = 4;
        String 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.";

        assertEquals(expected, foodChain.verse(verse));
    }


    @Test
    @Ignore("Remove to run test.")
    public void dog() {
        int verse = 5;
        String 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.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void goat() {
        int verse = 6;
        String 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.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void cow() {
        int verse = 7;
        String 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.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void horse() {
        int verse = 8;
        String expected = "I know an old lady who swallowed a horse.\n" +
                          "She's dead, of course!";

        assertEquals(expected, foodChain.verse(verse));
    }


    @Test
    @Ignore("Remove to run test.")
    public void multipleVerses() {
        int startVerse = 1;
        int endVerse = 3;
        String 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.";

        assertEquals(expected, foodChain.verses(startVerse, endVerse));
    }


    @Test
    @Ignore("Remove to run test.")
    public void wholeSong() {
        int startVerse = 1;
        int endVerse = 8;
        String 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!";

        assertEquals(expected, foodChain.verses(startVerse, endVerse));
    }
}
class FoodChain {
    private static final 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 final 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"};

    String verse(int verse) {
        if ((verse < 1) || (verse > 8)) {
            return "";
        }
        if (verse == 8) {
            return firstLines[7];
        }
        StringBuilder sb = new StringBuilder(firstLines[verse - 1]);
        for (int i = verse - 1; i >= 0; i--) {
            sb.append(lastLines[i]);
        }
        return sb.toString();
    }

    String verses(int startVerse, int endVerse) {
        if (startVerse > endVerse) {
            return "";
        }
        StringBuilder result = new StringBuilder(verse(startVerse));
        for (int i = startVerse + 1; i <= endVerse; i++) {
            result.append("\n\n");
            result.append(verse(i));
        }
        return result.toString();
    }
}

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?