Avatar of phamduc

phamduc's solution

to Kindergarten Garden in the Scala Track

Published at May 31 2019 · 0 comments
Test suite

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.

........................ # 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:


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:


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

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.


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.


import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.1 */
class KindergartenGardenTest extends FunSuite with Matchers {

  test("partial garden - garden with single student") {
    Garden.defaultGarden("RC\nGG").plants("Alice") should be(
      List(Plant.Radishes, Plant.Clover, Plant.Grass, Plant.Grass))

  test("partial garden - different garden with single student") {
    Garden.defaultGarden("VC\nRC").plants("Alice") should be(
      List(Plant.Violets, Plant.Clover, Plant.Radishes, Plant.Clover))

  test("partial garden - garden with two students") {
    Garden.defaultGarden("VVCG\nVVRC").plants("Bob") should be(
      List(Plant.Clover, Plant.Grass, Plant.Radishes, Plant.Clover))

    "multiple students for the same garden with three students, partial garden - second student's garden") {
    Garden.defaultGarden("VVCCGG\nVVCCGG").plants("Bob") should be(
      List(Plant.Clover, Plant.Clover, Plant.Clover, Plant.Clover))

    "multiple students for the same garden with three students, partial garden - third student's garden") {
    Garden.defaultGarden("VVCCGG\nVVCCGG").plants("Charlie") should be(
      List(Plant.Grass, Plant.Grass, Plant.Grass, Plant.Grass))

  test("full garden - first student's garden") {
      .plants("Alice") should be(
      List(Plant.Violets, Plant.Radishes, Plant.Violets, Plant.Radishes))

  test("full garden - second student's garden") {
      .plants("Bob") should be(
      List(Plant.Clover, Plant.Grass, Plant.Clover, Plant.Clover))

  test("full garden - second to last student's garden") {
      .plants("Kincaid") should be(
      List(Plant.Grass, Plant.Clover, Plant.Clover, Plant.Grass))

  test("full garden - last student's garden") {
      .plants("Larry") should be(
      List(Plant.Grass, Plant.Violets, Plant.Clover, Plant.Violets))
object Plant {

  sealed trait Species

  case object Grass extends Species

  case object Clover extends Species

  case object Radishes extends Species

  case object Violets extends Species

  def getSpecies(key: Char): Species = key match {
    case 'G' => Grass
    case 'C' => Clover
    case 'R' => Radishes
    case 'V' => Violets

case class Garden(vegetables: String, pupils: Map[String, Int]) {

  private val _qte = 2

  private def getPlants(initials: String, index: Int): List[Plant.Species] =
    for {
      i <- List.iterate(index, _qte)(_ + 1)
      initial = initials.charAt(i)
    } yield {

  def plants(name: String): List[Plant.Species] = {
    val rows = vegetables.split("\n").toList
    val index = pupils(name)
    rows.foldLeft(List[Plant.Species]()) {
      (acc, s) => acc ::: getPlants(s, index)


object Garden {
  val defauftPupils = Map(
    "Alice" -> 0,
    "Bob" -> 2,
    "Charlie" -> 4,
    "David" -> 6,
    "Eve" -> 8,
    "Fred" -> 10,
    "Ginny" -> 12,
    "Harriet" -> 14,
    "Ileana" -> 16,
    "Joseph" -> 18,
    "Kincaid" -> 20,
    "Larry" -> 22

  def defaultGarden(vegetables: String) = Garden(vegetables, defauftPupils)

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?