# paulfioravanti's solution

## to Kindergarten Garden in the Ruby Track

Published at May 07 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:

While asking for Bob's plants would yield:

• Clover, grass, clover, clover

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby kindergarten_garden_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride kindergarten_garden_test.rb
``````

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

### kindergarten_garden_test.rb

``````require 'minitest/autorun'
require_relative 'kindergarten_garden'

class GardenTest < Minitest::Test
def test_alices_garden
garden = Garden.new("RC\nGG")
assert_equal [:radishes, :clover, :grass, :grass], garden.alice
end

def test_different_garden_for_alice
skip
garden = Garden.new("VC\nRC")
assert_equal [:violets, :clover, :radishes, :clover], garden.alice
end

def test_bobs_garden
skip
garden = Garden.new("VVCG\nVVRC")
assert_equal [:clover, :grass, :radishes, :clover], garden.bob
end

def test_bob_and_charlies_gardens
skip
garden = Garden.new("VVCCGG\nVVCCGG")
assert_equal [:clover, :clover, :clover, :clover], garden.bob
assert_equal [:grass, :grass, :grass, :grass], garden.charlie
end
end

class TestFullGarden < Minitest::Test
def setup
skip
diagram = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV"
@garden = Garden.new(diagram)
end

def test_alice
skip
end

def test_bob
skip
assert_equal [:clover, :grass, :clover, :clover], garden.bob
end

def test_charlie
skip
assert_equal [:violets, :violets, :clover, :grass], garden.charlie
end

def test_david
skip
end

def test_eve
skip
assert_equal [:clover, :grass, :radishes, :grass], garden.eve
end

def test_fred
skip
assert_equal [:grass, :clover, :violets, :clover], garden.fred
end

def test_ginny
skip
assert_equal [:clover, :grass, :grass, :clover], garden.ginny
end

def test_harriet
skip
end

def test_ileana
skip
assert_equal [:grass, :clover, :violets, :clover], garden.ileana
end

def test_joseph
skip
assert_equal [:violets, :clover, :violets, :grass], garden.joseph
end

def test_kincaid
skip
assert_equal [:grass, :clover, :clover, :grass], garden.kincaid
end

def test_larry
skip
assert_equal [:grass, :violets, :clover, :violets], garden.larry
end
end

class DisorderedTest < Minitest::Test
def setup
skip
diagram = "VCRRGVRG\nRVGCCGCV"
students = %w(Samantha Patricia Xander Roger)
@garden = Garden.new(diagram, students)
end

def test_patricia
skip
assert_equal [:violets, :clover, :radishes, :violets], garden.patricia
end

def test_roger
skip
end

def test_samantha
skip
assert_equal [:grass, :violets, :clover, :grass], garden.samantha
end

def test_xander
skip
assert_equal [:radishes, :grass, :clover, :violets], garden.xander
end
end

class TwoGardensDifferentStudents < Minitest::Test
def diagram
"VCRRGVRG\nRVGCCGCV"
end

def garden_1
@garden_1 ||= Garden.new(diagram, %w(Alice Bob Charlie Dan))
end

def garden_2
@garden_2 ||= Garden.new(diagram, %w(Bob Charlie Dan Erin))
end

def test_bob_and_charlie_per_garden
skip
assert_equal [:violets, :clover, :radishes, :violets], garden_2.bob
assert_equal [:grass, :violets, :clover, :grass], garden_1.charlie
end
end``````
``````# frozen_string_literal: true

class Garden
PLANTS = {
"C" => :clover,
"G" => :grass,
"V" => :violets
}.freeze
private_constant :PLANTS
COLUMNS = (0..30).step(2).freeze
private_constant :COLUMNS
NEWLINE = "\n"
private_constant :NEWLINE
STUDENTS = %i[
alice
bob
charlie
david
eve
fred
ginny
harriet
ileana
joseph
kincaid
larry
patricia
roger
samantha
xander
].freeze
private_constant :STUDENTS

def initialize(plants, students = STUDENTS)
@plants = plants.split(NEWLINE).map(&:chars)
@students = students.sort
define_student_methods
end

private

def define_student_methods
students.zip(COLUMNS).each do |student, column|
define_singleton_method(student.downcase) do
fetch_student_plants(column)
end
end
end

def fetch_student_plants(column)
plants.flat_map { |row| row[column..column.succ].map(&PLANTS) }
end
end``````