Avatar of shmibs

shmibs's solution

to Kindergarten Garden in the Elixir Track

Published at Jul 13 2018 · 1 comment
Test suite


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.

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

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!"

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.


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.


if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("garden.exs", __DIR__)

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}

  @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}

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

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

  @tag :pending
  test "gets the garden for all students" do
    assert garden_info.alice == {:violets, :radishes, :violets, :radishes}
    assert garden_info.bob == {:clover, :grass, :clover, :clover}
    assert garden_info.charlie == {:violets, :violets, :clover, :grass}
    assert garden_info.david == {:radishes, :violets, :clover, :radishes}
    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.harriet == {:violets, :radishes, :radishes, :violets}
    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}

  @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 == {}

  @tag :pending
  test "gets the garden for all students with custom child names" do
    names = [

    garden_info = Garden.info(garden_string, names)
    assert garden_info.maggie == {:violets, :radishes, :violets, :radishes}
    assert garden_info.nate == {:clover, :grass, :clover, :clover}
    assert garden_info.ophelia == {:violets, :violets, :clover, :grass}
    assert garden_info.pete == {:radishes, :violets, :clover, :radishes}
    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.ursula == {:violets, :radishes, :radishes, :violets}
    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}
defmodule Garden do
  @default_names [
    :alice, :bob, :charlie, :david,
    :eve, :fred, :ginny, :harriet,
    :ileana, :joseph, :kincaid, :larry

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

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

  @spec info(String.t(), list) :: map
  def info(diagram, names \\ @default_names) do
    names = Enum.sort names
    m = Enum.reduce(names, %{}, &(Map.put_new &2, &1, {}) )
    String.split(diagram, "\n")
    |> Enum.reduce(m, &(parse_string &1, &2, names) )

  defp parse_string(s, m, n) do
    |> Stream.map( &(@c2p[&1]) )
    |> Stream.chunk(2)
    |> Stream.zip(n)
    |> Enum.reduce(m, &insertp/2)

  defp insertp({[p1, p2], n}, m) do
    t = Map.get(m, n) |> Tuple.append(p1) |> Tuple.append(p2)
    Map.put m, n, t

Community comments

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

can this be made any more flexible?

works on strings with N rows works on rows of varying lengths works when a name appears multiple times in the name list

no way to do row offsets, though, and the "assigned in alphabetical order" requirement means students' plants are always clumped together no matter where they appear in the provided list, which is annoying: iex(6)> Garden.info "VVGG\nVV\nVVGGRR", [:alice, :bob, :cora] %{alice: {:violets, :violets, :violets, :violets, :violets, :violets}, bob: {:grass, :grass, :grass, :grass}, cora: {:radishes, :radishes}} iex(7)> Garden.info "VVGG\nVV\nVVGGRR", [:alice, :bob, :alice] %{alice: {:violets, :violets, :grass, :grass, :violets, :violets, :violets, :violets, :grass, :grass}, bob: {:radishes, :radishes}}

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?