Avatar of Ric0chet

Ric0chet's solution

to Kindergarten Garden in the Scala Track

Published at Oct 09 2019 · 1 comment
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

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.

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.

GardenTest.scala

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") {
    pending
    Garden.defaultGarden("VC\nRC").plants("Alice") should be(
      List(Plant.Violets, Plant.Clover, Plant.Radishes, Plant.Clover))
  }

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

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

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

  test("full garden - first student's garden") {
    pending
    Garden
      .defaultGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
      .plants("Alice") should be(
      List(Plant.Violets, Plant.Radishes, Plant.Violets, Plant.Radishes))
  }

  test("full garden - second student's garden") {
    pending
    Garden
      .defaultGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
      .plants("Bob") should be(
      List(Plant.Clover, Plant.Grass, Plant.Clover, Plant.Clover))
  }

  test("full garden - second to last student's garden") {
    pending
    Garden
      .defaultGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
      .plants("Kincaid") should be(
      List(Plant.Grass, Plant.Clover, Plant.Clover, Plant.Grass))
  }

  test("full garden - last student's garden") {
    pending
    Garden
      .defaultGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
      .plants("Larry") should be(
      List(Plant.Grass, Plant.Violets, Plant.Clover, Plant.Violets))
  }
}
import Plant.Plant
//  10-09-19

object Garden {
private val kids = 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(seeds: String): Garden =
    new Garden(kids, seeds.split("\n").toList)
}

// 41 ms
class Garden(kids: Map[String, Int], rows: List[String]) {
  def plants(name: String): List[Plant] =
    rows.foldRight(List[Plant]()) { (row, acc) =>  // rt fold avoids reverse
      Plant(row.charAt(kids(name))) ::
        Plant(row.charAt(kids(name) + 1)) :: acc
    }
}

object Plant extends Enumeration {
  type Plant = Value

  val Clover: Plant.Value   = Value('C')
  val Grass: Plant.Value    = Value('G')
  val Radishes: Plant.Value = Value('R')
  val Violets: Plant.Value  = Value('V')
}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of Ric0chet

I changed the kids List to a Map, to make the plants method into a lambda expression--with no benchmark hit!

Also, I wasn't sure on this one if the kids should reside in the class or be passed in from the companion object, as above. It didn't seem complex enough to warrant its own Object, tho.

Ric0chet's Reflection

Unlike other tracks, the current Scala version has no provision or tests for bad input (mis-matched rows, more students than plots, etc.). So, I didn't add any validation or hardening.