using System.Collections.Generic;
namespace DailyProgrammer276
public static class Program
public static void Main()
var results = KeyFunction(
grouping => new KeyValuePair<string, string>(grouping.Key, grouping.Count().ToString())
BoxPrint(results, includeBorder: false);
var keysAndElements = GroupsToSumInput.Aggregate(
Tuple.Create(new List<string>(GroupsToSumInput.Length), new List<string>(GroupsToSumInput.Length)),
agg.Item1.Add(splits[0]);
agg.Item2.Add(splits[1]);
var sumResults = KeyFunction(
grouping => new KeyValuePair<string, string>(grouping.Key, grouping.Sum(int.Parse).ToString())
var nubResults = KeyFunction(
grouping => new KeyValuePair<string, string>(grouping.Key, grouping.First())
private static IEnumerable<KeyValuePair<T, T>> KeyFunction<T>(
Func<IGrouping<T, T>, KeyValuePair<T, T>> applyFunction)
var inputKvps = keys == null
? elements.Select(x => new KeyValuePair<T, T>(x, x))
: keys.Zip(elements, (key, element) => new KeyValuePair<T, T>(key, element));
var outputKvps = inputKvps
.GroupBy(x => x.Key, pair => pair.Value)
private static void BoxPrint(IReadOnlyCollection<KeyValuePair<string, string>> results, bool includeBorder = true)
var maxKeyLength = results.Max(x => x.Key.Length);
var maxValueLength = results.Max(x => x.Value.Length);
var topBuffer = Enumerable.Range(0, maxValueLength + maxKeyLength + 3).Select(_ => '─').ToArray();
var middleBuffer = topBuffer.ToArray();
var bottomBuffer = topBuffer.ToArray();
topBuffer.SetValue('┌', 0);
topBuffer.SetValue('┬', 1 + maxKeyLength);
topBuffer.SetValue('┐', topBuffer.Length - 1);
middleBuffer.SetValue('├', 0);
middleBuffer.SetValue('┼', 1 + maxKeyLength);
middleBuffer.SetValue('┤', topBuffer.Length - 1);
bottomBuffer.SetValue('└', 0);
bottomBuffer.SetValue('┴', 1 + maxKeyLength);
bottomBuffer.SetValue('┘', topBuffer.Length - 1);
var verticalSpacer = includeBorder ? '|' : ' ';
var bufferToWrite = topBuffer;
foreach (var keyValuePair in results)
Console.WriteLine(bufferToWrite);
Console.WriteLine($"{verticalSpacer}{keyValuePair.Key.PadLeft(maxKeyLength)}{verticalSpacer}{keyValuePair.Value.PadLeft(maxValueLength)}{verticalSpacer}");
bufferToWrite = middleBuffer;
bufferToWrite = bottomBuffer;
Console.WriteLine(bufferToWrite);
private const string HistogramInput =
"5 3 5 2 2 9 7 0 7 5 9 2 9 1 9 9 6 6 8 5 1 1 4 8 5 0 3 5 8 2 3 8 3 4 6 4 9 3 4 3 4 5 9 9 9 7 7 1 9 3 4 6 6 8 8 0 4 0 6 3 2 6 3 2 3 5 7 4 2 6 7 3 9 5 7 8 9 5 6 5 6 8 3 1 8 4 6 5 6 4 8 9 5 7 8 4 4 9 2 6 10";
private static readonly string[] GroupsToSumInput =