Avatar of artemkorsakov

artemkorsakov's solution

to House in the Java Track

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

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

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

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

import static org.junit.Assert.assertEquals;

public class HouseTest {

    private House house;

    @Before
    public void setup() {
        house = new House();
    }

    @Test
    public void verseOne() {
        String expected = "This is the house that Jack built.";
        int verse = 1;

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

    @Ignore("Remove to run test")
    @Test
    public void verseTwo() {
        String expected =
                "This is the malt " +
                "that lay in the house that Jack built.";
        int verse = 2;

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

    @Ignore("Remove to run test")
    @Test
    public void verseThree() {
        String expected =
                "This is the rat " +
                "that ate the malt " +
                "that lay in the house that Jack built.";
        int verse = 3;

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

    @Ignore("Remove to run test")
    @Test
    public void verseFour() {
        String expected =
                "This is the cat " +
                "that killed the rat " +
                "that ate the malt " +
                "that lay in the house that Jack built.";
        int verse = 4;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        int startVerse = 4;
        int endVerse = 8;

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

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

        assertEquals(expected, house.sing());
    }
}
class House {
    private static final String[] firstLines = new String[]{
            "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 final String[] lastLines = new String[]{
            "that lay in the house that Jack built.",
            "that ate the malt ",
            "that killed the rat ",
            "that worried the cat ",
            "that tossed the dog ",
            "that milked the cow with the crumpled horn ",
            "that kissed the maiden all forlorn ",
            "that married the man all tattered and torn ",
            "that woke the priest all shaven and shorn ",
            "that kept the rooster that crowed in the morn ",
            "that belonged to the farmer sowing his corn "};

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

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

    String sing() {
        return verses(1, 12);
    }

}

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?