artemkorsakov's solution

to Kindergarten Garden in the C# Track

Published at Mar 16 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

Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit KindergartenGarden.cs`

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Random musings during airplane trip. http://jumpstartlab.com

KindergartenGardenTest.cs

``````// This file was auto-generated based on version 1.1.1 of the canonical data.

using Xunit;

public class KindergartenGardenTest
{
[Fact]
public void Partial_garden_garden_with_single_student()
{
var sut = new KindergartenGarden("RC\nGG");
Assert.Equal(new[] { Plant.Radishes, Plant.Clover, Plant.Grass, Plant.Grass }, sut.Plants("Alice"));
}

[Fact(Skip = "Remove to run test")]
public void Partial_garden_different_garden_with_single_student()
{
var sut = new KindergartenGarden("VC\nRC");
Assert.Equal(new[] { Plant.Violets, Plant.Clover, Plant.Radishes, Plant.Clover }, sut.Plants("Alice"));
}

[Fact(Skip = "Remove to run test")]
public void Partial_garden_garden_with_two_students()
{
var sut = new KindergartenGarden("VVCG\nVVRC");
Assert.Equal(new[] { Plant.Clover, Plant.Grass, Plant.Radishes, Plant.Clover }, sut.Plants("Bob"));
}

[Fact(Skip = "Remove to run test")]
public void Partial_garden_multiple_students_for_the_same_garden_with_three_students_second_students_garden()
{
var sut = new KindergartenGarden("VVCCGG\nVVCCGG");
Assert.Equal(new[] { Plant.Clover, Plant.Clover, Plant.Clover, Plant.Clover }, sut.Plants("Bob"));
}

[Fact(Skip = "Remove to run test")]
public void Partial_garden_multiple_students_for_the_same_garden_with_three_students_third_students_garden()
{
var sut = new KindergartenGarden("VVCCGG\nVVCCGG");
Assert.Equal(new[] { Plant.Grass, Plant.Grass, Plant.Grass, Plant.Grass }, sut.Plants("Charlie"));
}

[Fact(Skip = "Remove to run test")]
public void Full_garden_first_students_garden()
{
var sut = new KindergartenGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV");
}

[Fact(Skip = "Remove to run test")]
public void Full_garden_second_students_garden()
{
var sut = new KindergartenGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV");
Assert.Equal(new[] { Plant.Clover, Plant.Grass, Plant.Clover, Plant.Clover }, sut.Plants("Bob"));
}

[Fact(Skip = "Remove to run test")]
public void Full_garden_second_to_last_students_garden()
{
var sut = new KindergartenGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV");
Assert.Equal(new[] { Plant.Grass, Plant.Clover, Plant.Clover, Plant.Grass }, sut.Plants("Kincaid"));
}

[Fact(Skip = "Remove to run test")]
public void Full_garden_last_students_garden()
{
var sut = new KindergartenGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV");
Assert.Equal(new[] { Plant.Grass, Plant.Violets, Plant.Clover, Plant.Violets }, sut.Plants("Larry"));
}
}``````
``````﻿using System;
using System.Collections.Generic;
using System.Linq;

public enum Plant
{
Violets,
Clover,
Grass
}

public enum Child
{
Alice, Bob, Charlie, David,
Eve, Fred, Ginny, Harriet,
Ileana, Joseph, Kincaid, Larry
}

public class KindergartenGarden
{
private readonly List<string> _plantsList = new List<string>();

public KindergartenGarden(string diagram)
{
var rows = diagram.Split('\n');
if (rows.Length != 2 || rows[0].Length != rows[1].Length)
{
throw new ArgumentException("Invalid diagram");
}

for (var i = 0; i < rows[0].Length / 2; i++)
{
var plants = rows[0].Substring(2 * i, 2) +
rows[1].Substring(2 * i, 2);
}
}

public IEnumerable<Plant> Plants(string student)
{
var index = (int)(Child)Enum.Parse(typeof(Child), student);
if (index > _plantsList.Count - 1)
{
throw new ArgumentException("Invalid student");
}
var plants = _plantsList[index];
}

private static IEnumerable<Plant> ToPlants(string plant)
{
return plant.ToCharArray().Select(ToPlant);
}

private static Plant ToPlant(char plant)
{
switch (plant)
{
case 'V':
return Plant.Violets;
case 'R':
case 'C':
return Plant.Clover;
case 'G':
return Plant.Grass;
default:
throw new ArgumentException("Invalid plant");
}
}
}``````