Avatar of oleg-zinovev

oleg-zinovev's solution

to Kindergarten Garden in the Scala Track

Published at Oct 04 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.

Please see the learning and installation pages if you need any help.


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 Garden {
  def defaultGarden(value: String): Garden = new Garden(value.split("\n").map(_.grouped(2).toSeq))


class Garden private(private val value: Array[Seq[String]]) {
  private val names = List("Alice", "Bob", "Charlie", "David", "Eve", "Fred", "Ginny", "Harriet", "Ileana", "Joseph", "Kincaid", "Larry")

  def plants(name: String): List[Plant.PlantValue] = {
    val index = names.indexOf(name)

    if (index < 0) {
    } else {
      (value(0)(index) ++ value(1)(index))
          case Plant.Radishes.char => Plant.Radishes
          case Plant.Clover.char => Plant.Clover
          case Plant.Grass.char => Plant.Grass
          case Plant.Violets.char => Plant.Violets

object Plant extends Enumeration {

  sealed case class PlantValue(char: Char) extends Val

  val Radishes = PlantValue('R')
  val Clover = PlantValue('C')
  val Grass = PlantValue('G')
  val Violets = PlantValue('V')

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?