Avatar of artemkorsakov

artemkorsakov's solution

to Kindergarten Garden in the Java Track

Published at Mar 16 2019 · 0 comments
Instructions
Test suite
Solution

Given a diagram, determine which plants each child in the kindergarten class is responsible for.

The kindergarten class is learning about growing plants. The teacher thought it would be a good idea to give them actual seeds, plant them in actual dirt, and grow actual plants.

They've chosen to grow grass, clover, radishes, and violets.

To this end, the children have put little cups along the window sills, and planted one type of plant in each cup, choosing randomly from the available types of seeds.

[window][window][window]
........................ # each dot represents a cup
........................

There are 12 children in the class:

  • Alice, Bob, Charlie, David,
  • Eve, Fred, Ginny, Harriet,
  • Ileana, Joseph, Kincaid, and Larry.

Each child gets 4 cups, two on each row. Their teacher assigns cups to the children alphabetically by their names.

The following diagram represents Alice's plants:

[window][window][window]
VR......................
RG......................

In the first row, nearest the windows, she has a violet and a radish. In the second row she has a radish and some grass.

Your program will be given the plants from left-to-right starting with the row nearest the windows. From this, it should be able to determine which plants belong to each student.

For example, if it's told that the garden looks like so:

[window][window][window]
VRCGVVRVCGGCCGVRGCVCGCGV
VRCCCGCRRGVCGCRVVCVGCGCV

Then if asked for Alice's plants, it should provide:

  • Violets, radishes, violets, radishes

While asking for Bob's plants would yield:

  • Clover, grass, clover, clover

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

Random musings during airplane trip. http://jumpstartlab.com

Submitting Incomplete Solutions

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

KindergartenGardenTest.java

import java.util.List;
import java.util.Arrays;

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

import static org.junit.Assert.assertEquals;

public class KindergartenGardenTest {

    @Test
    public void singleStudent() {
        String student = "Alice";
        String plants = "RC\nGG";
        List<Plant> expected = Arrays.asList(Plant.RADISHES, Plant.CLOVER, Plant.GRASS, Plant.GRASS);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void singleStudent2() {
        String student = "Alice";
        String plants = "VC\nRC";
        List<Plant> expected = Arrays.asList(Plant.VIOLETS, Plant.CLOVER, Plant.RADISHES, Plant.CLOVER);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void twoStudents() {
        String student = "Bob";
        String plants = "VVCG\nVVRC";
        List<Plant> expected = Arrays.asList(Plant.CLOVER, Plant.GRASS, Plant.RADISHES, Plant.CLOVER);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void oneGardenSecondStudent() {
        String student = "Bob";
        String plants = "VVCCGG\nVVCCGG";
        List<Plant> expected = Arrays.asList(Plant.CLOVER, Plant.CLOVER, Plant.CLOVER, Plant.CLOVER);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void oneGardenThirdStudent() {
        String student = "Charlie";
        String plants = "VVCCGG\nVVCCGG";
        List<Plant> expected = Arrays.asList(Plant.GRASS, Plant.GRASS, Plant.GRASS, Plant.GRASS);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void fullGardenFirstStudent() {
        String student = "Alice";
        String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
        List<Plant> expected = Arrays.asList(Plant.VIOLETS, Plant.RADISHES, Plant.VIOLETS, Plant.RADISHES);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void fullGardenSecondStudent() {
        String student = "Bob";
        String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
        List<Plant> expected = Arrays.asList(Plant.CLOVER, Plant.GRASS, Plant.CLOVER, Plant.CLOVER);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void fullGardenSecondToLastStudent() {
        String student = "Kincaid";
        String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
        List<Plant> expected = Arrays.asList(Plant.GRASS, Plant.CLOVER, Plant.CLOVER, Plant.GRASS);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void fullGardenLastStudent() {
        String student = "Larry";
        String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
        List<Plant> expected = Arrays.asList(Plant.GRASS, Plant.VIOLETS, Plant.CLOVER, Plant.VIOLETS);

        assertEquals(
            expected,
            new KindergartenGarden(plants).getPlantsOfStudent(student)
        );
    }

}
import java.util.ArrayList;
import java.util.List;

enum Student {
    ALICE, BOB, CHARLIE, DAVID, EVE, FRED, GINNY, HARRIET, ILEANA, JOSEPH, KINCAID, LARRY
}

class KindergartenGarden {
    private List<String> plants = new ArrayList<>();

    KindergartenGarden(String garden) {
        String[] rows = garden.split("\n");
        if (rows.length != 2 || rows[0].length() != rows[1].length()) {
            return;
        }
        for (int i = 0; i < rows[0].length() / 2; i++) {
            String plant = rows[0].substring(2 * i, 2 * i + 2) + rows[1].substring(2 * i, 2 * i + 2);
            plants.add(plant);
        }
    }

    List<Plant> getPlantsOfStudent(String student) {
        int index = Student.valueOf(student.toUpperCase()).ordinal();
        List<Plant> result = new ArrayList<>();
        for (char pl : plants.get(index).toCharArray()) {
            result.add(Plant.getPlant(pl));
        }
        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?