# rootulp's solution

## to Kindergarten Garden in the Java Track

Published at Jul 13 2018 · 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:

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
``````

## 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";

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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class KindergartenGarden {

private static final String[] DEFAULT_STUDENTS = { "Alice", "Bob", "Charlie", "David", "Eve", "Fred", "Ginny", "Harriet", "Ileana", "Joseph", "Kincaid", "Larry"};

private final Plant[][] plants;
private final List<String> students;

public KindergartenGarden(String diagram) {
this(diagram, DEFAULT_STUDENTS);
}

public KindergartenGarden(String diagram, String[] students) {
this.plants = parseDiagram(diagram);
this.students = Arrays.asList(students);
Collections.sort(this.students);
}

public List<Plant> getPlantsOfStudent(String student) {
return getPlantsForIndex(getAlphabeticIndex(student));
}

private int getAlphabeticIndex(String student) {
return students.indexOf(student);
}

private List<Plant> getPlantsForIndex(int index) {
return Arrays.asList(plants[0][index * 2],
plants[0][index * 2 + 1],
plants[1][index * 2],
plants[1][index * 2 + 1]);
}

private Plant[][] parseDiagram(String diagram) {
final int rows = 2;
final int cols = diagram.length() / 2;
Plant[][] plants = new Plant[rows][cols];
Scanner sc = new Scanner(diagram.replaceAll("\n","")).useDelimiter("");
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
plants[r][c] = Plant.getPlant(sc.next().charAt(0));
}
}
return plants;
}

}``````