using System; using System.Collections.Generic; using System.Linq; public enum Plant { Violets, Radishes, Clover, Grass } public class Garden { private const int PlantsPerChildPerRow = 2; private const char RowSeparator = '\n'; private static readonly string[] DefaultChildren = { "Alice", "Bob", "Charlie", "David", "Eve", "Fred", "Ginny", "Harriet", "Ileana", "Joseph", "Kincaid", "Larry" }; private static readonly IDictionary PlantCodesToPlants = new Dictionary { { 'V', Plant.Violets }, { 'R', Plant.Radishes }, { 'C', Plant.Clover }, { 'G', Plant.Grass } }; private readonly IDictionary> plantsByChild; public Garden(IEnumerable children, string windowSills) { var plantsPerChild = PlantsPerChild(windowSills); plantsByChild = children.OrderBy(c => c) .Zip(plantsPerChild, Tuple.Create) .ToDictionary(kv => kv.Item1, kv => kv.Item2); } public IEnumerable GetPlants(string child) { return plantsByChild.ContainsKey(child) ? plantsByChild[child] : Enumerable.Empty(); } public static Garden DefaultGarden(string windowSills) { return new Garden(DefaultChildren, windowSills); } private static Plant PlantFromCode(char code) { return PlantCodesToPlants[code]; } private static IEnumerable> PlantsPerChild(string windowSills) { var row = windowSills.Split(RowSeparator).Select(PlantsInRow).ToArray(); return row[0].Zip(row[1], (row1Plants, row2Plants) => row1Plants.Concat(row2Plants)); } private static IEnumerable> PlantsInRow(string row) { return Partition(row.Select(PlantFromCode), PlantsPerChildPerRow); } private static IEnumerable> Partition(IEnumerable input, int partitionSize) { return input.Select((item, inx) => new { item, inx }) .GroupBy(x => x.inx / partitionSize) .Select(g => g.Select(x => x.item)); } }