🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of thekeele

thekeele's solution

to Kindergarten Garden in the Elixir Track

Published at Nov 06 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution 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:

  • 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
  @students [
    :alice, :bob, :charlie, :david, :eve, :fred, :ginny, :harriet, :ileana, :joseph, :kincaid, :larry
  ]

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

  @spec info(String.t(), list) :: map
  def info(info_string, student_names \\ @students) do
    students_plants = Enum.into(student_names, %{}, &{&1, {}})

    process_windows(students_plants, Map.keys(students_plants), String.split(info_string, "\n"))
  end

  defp process_windows(students_plants, _, ["", ""]), do: students_plants

  defp process_windows(
       students_plants,
       [student_name | students],
       [
        <<row_one::binary-size(2), rest_row_one::binary()>>,
        <<row_two::binary-size(2), rest_row_two::binary()>>
       ]) do
    students_plants = %{students_plants | student_name => to_plant_names(row_one <> row_two)}
    process_windows(students_plants, students, [rest_row_one, rest_row_two])
  end

  defp to_plant_names(plant_codes) do
    plant_codes |> String.codepoints() |> Enum.map(&@plant_codes[&1]) |> List.to_tuple()
  end
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?