// Exercise 1: word frequency with Dictionary + HashSet (easy) // // Implement: // - CountWords(text): split on whitespace and punctuation (use the // provided Separators array), lowercase each word, ignore empty // strings, and return a Dictionary of word -> count. // Use TryGetValue (or GetValueOrDefault) — never index a missing key. // - UniqueInOrder(words): return a List containing each word // the FIRST time it appears, preserving order. Use a HashSet // for the "seen" check so the whole method is O(n), not O(n^2). // // Run with: dotnet run (inside a console project) or paste into a Main method. using System; using System.Collections.Generic; class Program { static readonly char[] Separators = { ' ', '\t', '\n', ',', '.', '!', '?', ';', ':' }; #pragma warning disable IDE0060 static Dictionary CountWords(string text) { // TODO: split, lowercase, count return new Dictionary(); } static List UniqueInOrder(IEnumerable words) { // TODO: HashSet seen; add to result only when seen.Add(w) returns true return new List(); } #pragma warning restore IDE0060 static void Main() { int pass = 0, total = 0; var counts = CountWords("The cat. The dog! the END, end?"); total++; if (counts.Count == 4 && counts["the"] == 3 && counts["end"] == 2 && counts["cat"] == 1 && counts["dog"] == 1) { Console.WriteLine("PASS: CountWords"); pass++; } else Console.WriteLine("FAIL: CountWords, got " + string.Join(", ", counts)); total++; if (CountWords(" ").Count == 0) { Console.WriteLine("PASS: blank input"); pass++; } else Console.WriteLine("FAIL: blank input should give empty dictionary"); var unique = UniqueInOrder(new[] { "b", "a", "b", "c", "a" }); total++; if (string.Join(",", unique) == "b,a,c") { Console.WriteLine("PASS: UniqueInOrder"); pass++; } else Console.WriteLine("FAIL: UniqueInOrder, got " + string.Join(",", unique)); Console.WriteLine($"--- {pass}/{total} checks passed ---"); } }