Advertisement
DimitarP

Untitled

Sep 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace AL4
  8. {
  9. class Program
  10. {
  11. static int compareValues(KeyValuePair<string, int> a, KeyValuePair<string, int> b)
  12. {
  13. return b.Value.CompareTo(a.Value);
  14. }
  15. static void Main(string[] args)
  16. {
  17. string[] inputWords = Console.ReadLine().Split(' ').ToArray();
  18. int inputWordsCount = inputWords.Length;
  19. List<KeyValuePair<string, int>> wordsAndCounts = new List<KeyValuePair<string, int>>();
  20. foreach(string word in inputWords)
  21. {
  22. if(wordsAndCounts.Exists(kvp => kvp.Key == word))
  23. {
  24. int currCount = wordsAndCounts.FirstOrDefault(kvp => kvp.Key == word).Value;
  25. wordsAndCounts.Remove(wordsAndCounts.FirstOrDefault(kvp => kvp.Key == word));
  26. KeyValuePair<string, int> newCounterPair = new KeyValuePair<string, int>(word, currCount + 1);
  27. wordsAndCounts.Add(newCounterPair);
  28. }
  29. else
  30. {
  31. KeyValuePair<string, int> newPair = new KeyValuePair<string, int>(word, 1);
  32. wordsAndCounts.Add(newPair);
  33.  
  34. }
  35. }
  36. var sortedWordsByCounts = wordsAndCounts.OrderByDescending(kvp => kvp.Value);
  37. //wordsAndCounts.Sort(compareValues);
  38. foreach (var pair in sortedWordsByCounts)
  39. {
  40. double percentage = (double)pair.Value / inputWordsCount * 100;
  41. Console.WriteLine($"{pair.Key} -> {pair.Value} times ({percentage:F2}%)");
  42. }
  43.  
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement