Ejemplo n.º 1
0
        public void RunQuestionTwo(ICharacterReader[] readers, IOutputResult output)
        {
            var wordCount = new Dictionary <string, int>();

            //   string line = "a list of word frequencies ordered by word count and then alphabetically";
            string line = readers.ToString();

            while ((line != null))
            {
                var words = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                foreach (var word in words)
                {
                    if (wordCount.ContainsKey(word))
                    {
                        wordCount[word] = wordCount[word] + 1;
                    }
                    else
                    {
                        wordCount.Add(word, 1);
                        output.AddResult(wordCount.ToString());
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void RunQuestionOne(ICharacterReader reader, IOutputResult output)
        {
            // var text = "It was the best of times, it was the worst of times".ToLower();
            var text = reader.ToString();

            var match = Regex.Match(text, "\\w+");
            Dictionary <string, int> freq = new Dictionary <string, int>();

            while (match.Success)
            {
                string word = match.Value;
                if (freq.ContainsKey(word))
                {
                    freq[word]++;
                }
                else
                {
                    freq.Add(word, 1);
                }

                match = match.NextMatch();
            }

            Console.WriteLine("Rank  Word  Frequency Details");

            int rank = 1;

            foreach (var elem in freq.OrderByDescending(a => a.Value).Take(10))
            {
                Console.WriteLine("{0,2}    {1,-4}    {2,5}", rank++, elem.Key, elem.Value);
                output.AddResult(elem.ToString());
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// A task that allows the creation of an output in the required format.
        /// </summary>
        /// <param name="pairs">An ordered and enumerable collection of key/value pairs, in this case string/int pairs,
        /// containing the data that will be used for the creation of the output.</param>
        /// <param name="output">An object of a class that implements the IOutputResult interface.</param>
        /// <returns>The task that creates the output.</returns>
        private Task CreateOutput(IOrderedEnumerable <KeyValuePair <string, int> > pairs, IOutputResult output)
        {
            return(Task.Run(() =>
            {
                // The following tow lines are only used in verbose mode.
                if (verboseMode)
                {
                    Console.WriteLine("Output:");
                }
                if (verboseMode)
                {
                    Console.WriteLine("--------------------");
                }

                foreach (var pair in pairs)
                {
                    output.AddResult($"{pair.Key} - {pair.Value.ToString()}");
                }

                // The following line is only used in verbose mode.
                if (verboseMode)
                {
                    Console.WriteLine("--------------------");
                }
            }));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates an output in the desirable format.
 /// </summary>
 /// <param name="pairs">An ordered and enumerable collection of key/value pairs, in this case string/int pairs.</param>
 /// <param name="output">An object of a class that implements the IOutputResult interface.</param>
 private void CreateOutput(IOrderedEnumerable <KeyValuePair <string, int> > pairs, IOutputResult output)
 {
     foreach (var pair in pairs)
     {
         output.AddResult($"{pair.Key} - {pair.Value.ToString()}");
     }
 }
 private void SendWordsOrdered(IOutputResult output, Dictionary <string, int> wordCount)
 {
     lock (_padlock)
     {
         foreach (var item in wordCount.OrderBy(o => o.Key).OrderByDescending(o => o.Value))
         {
             output.AddResult(item.Key + " - " + item.Value);
         }
     }
 }
Ejemplo n.º 6
0
        public Task Print(IOutputResult output)
        {
            foreach (var keyValuePair in _dictionary.Dictionary
                     .OrderByDescending(x => x.Value)
                     .ThenBy(x => x.Key))
            {
                {
                    output.AddResult($"{keyValuePair.Key} - {keyValuePair.Value}");
                }
            }

            return(Task.FromResult(0));
        }