Avatar of paulfioravanti

paulfioravanti's solution

to Kindergarten Garden in the Elixir Track

Published at Aug 24 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:

  • Violets, radishes, violets, radishes

While asking for Bob's plants would yield:

  • Clover, grass, clover, clover

Running tests

Execute the tests with:

$ mix test

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

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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

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.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}
  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.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}
  end
end

test_helper.exs

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule Garden do
  @student_cups_per_row 2
  @plants %{
    ?C => :clover,
    ?G => :grass,
    ?R => :radishes,
    ?V => :violets
  }
  @default_student_names ~w[
    alice
    bob
    charlie
    david
    eve
    fred
    ginny
    harriet
    ileana
    joseph
    kincaid
    larry
  ]a

  @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(info_string, student_names \\ @default_student_names) do
    plants = generate_plants(info_string)

    student_names
    |> Enum.sort()
    |> padded_zip(plants)
    |> Enum.into(%{})
  end

  defp generate_plants(info_string) do
    info_string
    |> String.split("\n")
    |> Enum.map(&to_plant_cup_sets/1)
    |> Enum.zip()
    |> Enum.map(&cup_sets_to_tuple/1)
  end

  defp to_plant_cup_sets(plants_string) do
    plants_string
    |> String.to_charlist()
    |> Enum.map(&@plants[&1])
    |> Enum.chunk_every(@student_cups_per_row)
  end

  defp cup_sets_to_tuple({cup_set1, cup_set2}) do
    (cup_set1 ++ cup_set2)
    |> List.to_tuple()
  end

  defp padded_zip([student | students_tail], [plant_cup | plant_cups_tail]) do
    [{student, plant_cup} | padded_zip(students_tail, plant_cups_tail)]
  end

  defp padded_zip([student | students_tail], []) do
    [{student, {}} | padded_zip(students_tail, [])]
  end

  defp padded_zip([], _plant_cups), do: []
end

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?