/// <summary> /// Calculates the Duplicate Word occurence in a string response and the count /// </summary> /// <param name="response"></param> private static void CalculateDuplicateWords(string response, WordStatistics statistics) { var keyValuePairs = response.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).GroupBy(word => word) .ToDictionary(kvp => kvp.Key, kvp => kvp.Count()); statistics.DuplicateWord = keyValuePairs.Aggregate((l, r) => l.Value > r.Value ? l : r).Key; statistics.DuplicateWordCount = keyValuePairs[statistics.DuplicateWord]; }
/// <summary> /// How many of each letter are in the file /// How many letters are capitalized in the file /// The most common word and the number of times it has been seen. /// The most common 2 character prefix and the number of occurrences in the text file. /// </summary> /// <param name="args"></param> static void Main(string[] args) { //1. Get the file contents from the URL string url = @"https://ringba-test-html.s3-us-west-1.amazonaws.com/TestQuestions/output.txt"; var queryUrlTask = MakeAsyncRequest(url); var response = queryUrlTask.Result; var statistics = new WordStatistics(); //2. Split the words based on Camelcase response = SplitWordsBasedOnCamelCase(response); //3. Count the number of letters in the response statistics.LetterCount = CalculateLetterCount(response); //4. Count the number of occurences where the letter is capitalized statistics.CapitalizedLetterCount = CalculateCapitalizedLetterCount(response); //5. Count the duplicates and occurences. CalculateDuplicateWords(response, statistics); //6. Most common prefix 2 character and occurence //statistics.CommonPrefix = CommonPrefix(response); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("File Content Statistics"); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine(String.Format("Total Letters = {0,10}", statistics.LetterCount)); Console.WriteLine(String.Format("Letters in Uppercase = {0,10}", statistics.CapitalizedLetterCount)); Console.WriteLine(String.Format("Duplicate words = {0,10}", statistics.DuplicateWord)); Console.WriteLine(String.Format("Duplicate word count = {0,10}", statistics.DuplicateWordCount)); //Console.WriteLine(String.Format("Smallest prefix = {0,10}", statistics.CommonPrefix)); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("End of Program"); }