Avatar of AkashKumar78

AkashKumar78's solution

to Kindergarten Garden in the C# Track

Published at Sep 27 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 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");
        Assert.Equal(new[] { Plant.Violets, Plant.Radishes, Plant.Violets, Plant.Radishes }, sut.Plants("Alice"));
    }

    [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 = 'V',
    Radishes = 'R',
    Clover = 'C',
    Grass = 'G'
}

public class KindergartenGarden
{     
    private readonly string[] Diagram;
	private readonly List<string> Student = new List<string>()
	{ "Alice", "Bob", "Charlie", "David", "Eve", "Frade", "Ginney", "Harriet", "Ileana", "Joseph", "Kincaid", "Larry" };
	
    public KindergartenGarden(string diagram)
    {
	   this.Diagram = diagram.Split('\n');
    }
    
	private string FourPlants(string student)
	{
	    string fourPlants = string.Empty;
		foreach (var item in this.Diagram)
		{
		    fourPlants += item.Substring(this.Student.IndexOf(student) * 2, 2);
		}
		return fourPlants;
	}
    public IEnumerable<Plant> Plants(string student)
    {
        foreach (var plant in FourPlants(student))
        {
		   foreach (var item in Enum.GetValues(typeof(Plant)).Cast<Plant>())
		   {
		      if((char)item == plant)
			  {
			     yield return item;
			  }
			  
		   }
		}		
    }
	
}

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?