Avatar of specialjcg

specialjcg's solution

to Food Chain in the Java Track

Published at Sep 30 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!

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

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));
    }
}
public class FoodChain {
    private String[] vers ={"fly","spider","bird","cat","dog","goat","cow"

    };
    private String[] versInter ={"","It wriggled and jiggled and tickled inside her.\n","How absurd to swallow a bird!\n","Imagine that, to swallow a cat!\n",
            "What a hog, to swallow a dog!\n", "Just opened her throat and swallowed a goat!\n","I don't know how she swallowed a cow!\n"};
    public String verse(int verse) {
        final int verseVers = verse - 1;
        if (verse==8) return "I know an old lady who swallowed a horse.\n" +
                "She's dead, of course!";;
        return "I know an old lady who swallowed a "+vers[verseVers]+".\n"+versInter[verseVers]+ toCatch(verseVers)+"I don't know why she swallowed the fly. Perhaps she'll die.";
    }
private String toCatch(int verse){
        StringBuilder versTocatch= new StringBuilder();
    for (int i=verse;i>0;i--){
        versTocatch.append("She swallowed the ").append(vers[i]).append(" to catch the ").append(vers[i - 1]).append(vers[i - 1].equals("spider") ? " that wriggled and jiggled and tickled inside her.\n" : ".\n");}
     return versTocatch.toString();
}
    public String verses(int startVerse, int endVerse) {
        StringBuilder parseSing= new StringBuilder();
        for (int i=startVerse;i<endVerse;i++) parseSing.append(verse(i).concat("\n\n"));
        parseSing.append(verse(endVerse));
        return parseSing.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?