# davearonson's solution

## to Kindergarten Garden in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 tests

Execute the tests with:

``````\$ elixir kindergarten_garden_test.exs
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

For more detailed information about the Elixir track, please see the help page.

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

### garden_test.exs

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule GardenTest do
use ExUnit.Case

test "gets the garden for Alice with just her plants" do
garden_info = Garden.info("RC\nGG")
assert garden_info.alice == {:radishes, :clover, :grass, :grass}
end

@tag :pending
test "gets another garden for Alice with just her plants" do
garden_info = Garden.info("VC\nRC")
assert garden_info.alice == {:violets, :clover, :radishes, :clover}
end

@tag :pending
test "returns an empty tuple if the child has no plants" do
garden_info = Garden.info("VC\nRC")
assert garden_info.bob == {}
end

@tag :pending
test "gets the garden for Bob" do
garden_info = Garden.info("VVCG\nVVRC")
assert garden_info.bob == {:clover, :grass, :radishes, :clover}
end

@tag :pending
test "gets the garden for all students" do
garden_info = Garden.info("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
assert garden_info.bob == {:clover, :grass, :clover, :clover}
assert garden_info.charlie == {:violets, :violets, :clover, :grass}
assert garden_info.eve == {:clover, :grass, :radishes, :grass}
assert garden_info.fred == {:grass, :clover, :violets, :clover}
assert garden_info.ginny == {:clover, :grass, :grass, :clover}
assert garden_info.ileana == {:grass, :clover, :violets, :clover}
assert garden_info.joseph == {:violets, :clover, :violets, :grass}
assert garden_info.kincaid == {:grass, :clover, :clover, :grass}
assert garden_info.larry == {:grass, :violets, :clover, :violets}
end

@tag :pending
test "accepts custom child names" do
garden_info = Garden.info("VC\nRC", [:nate, :maggie])
assert garden_info.maggie == {:violets, :clover, :radishes, :clover}
assert garden_info.nate == {}
end

@tag :pending
test "gets the garden for all students with custom child names" do
names = [
:maggie,
:nate,
:xander,
:ophelia,
:pete,
:reggie,
:sylvia,
:tanner,
:ursula,
:victor,
:winnie,
:ynold
]

garden_string = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV"
garden_info = Garden.info(garden_string, names)
assert garden_info.nate == {:clover, :grass, :clover, :clover}
assert garden_info.ophelia == {:violets, :violets, :clover, :grass}
assert garden_info.reggie == {:clover, :grass, :radishes, :grass}
assert garden_info.sylvia == {:grass, :clover, :violets, :clover}
assert garden_info.tanner == {:clover, :grass, :grass, :clover}
assert garden_info.victor == {:grass, :clover, :violets, :clover}
assert garden_info.winnie == {:violets, :clover, :violets, :grass}
assert garden_info.xander == {:grass, :clover, :clover, :grass}
assert garden_info.ynold == {:grass, :violets, :clover, :violets}
end
end``````
``````defmodule Garden do
@doc """
Accepts a string representing the arrangement of cups on a windowsill and a
list with names of students in the class. The student names list does not
have to be in alphabetical order.

It decodes that string into the various gardens for each student and returns
that information in a map.
"""

@default_kids ~w(alice bob charlie david eve fred ginny harriet ileana joseph kincaid larry)a

@spec info(String.t(), list) :: map
def info(info_string, student_names \\ @default_kids) do
[row1, row2] = String.split(info_string, "\n")
do_match_with_flowers(Enum.sort(student_names),
String.graphemes(row1),
String.graphemes(row2),
%{})
end

defp do_match_with_flowers([], [], [], sofar), do: sofar

defp do_match_with_flowers([kid|rest], [], [], sofar) do
do_match_with_flowers(rest, [], [], Map.put(sofar, kid, {}))
end

defp do_match_with_flowers([kid|rest], [f1|[f2|row1]], [f3|[f4|row2]], sofar) do
do_match_with_flowers(rest, row1, row2,
Map.put(sofar, kid,
[f1, f2, f3, f4]
|> Enum.map(&(to_flower(&1)))
|> List.to_tuple))
end

@flowers %{ "G" => :grass, "C" => :clover, "R" => :radishes, "V" => :violets }

defp to_flower(char), do: @flowers[char]

end``````