Aliendreamer

histogram

Jul 13th, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. sing System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04ArrayAndListsExcercisesArrayHistogram
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<string> words = new List<string>();
  14.             List<string> inputString = Console.ReadLine().Split(' ').ToList();
  15.             List<int> counts = new List<int>();
  16.  
  17.             for (int i = 0; i < inputString.Count; i++)
  18.             {
  19.  
  20.                 if(!words.Contains(inputString[i]))
  21.                 {
  22.                     words.Add(inputString[i]);
  23.                     counts.Add(1);
  24.  
  25.                 }
  26.                 else
  27.                 {
  28.                     int index = words.IndexOf(inputString[i]);
  29.                     counts[index]++;
  30.                 }
  31.  
  32.             }
  33.             for (int i = 0; i < counts.Count - 1; i++)
  34.             {
  35.  
  36.  
  37.                 int index = i + 1;
  38.                 while (index > 0)
  39.                 {
  40.  
  41.                     if (counts[index - 1] < counts[index])
  42.                     {
  43.                         int tempCount = counts[index];
  44.                         counts[index] = counts[index - 1];
  45.                         counts[index - 1] = tempCount;
  46.  
  47.  
  48.                         string wordCount = words[index];
  49.                         words[index] = words[index - 1];
  50.                         words[index - 1] = wordCount;
  51.  
  52.  
  53.  
  54.                     }
  55.                     index--;
  56.  
  57.                 }
  58.                
  59.  
  60.  
  61.             }
  62.  
  63.             for (int i = 0; i < words.Count; i++)
  64.             {
  65.                 Console.WriteLine("{0} -> {1} times ({2:f2}%)", words[i], counts[i], (((double)counts[i] / (double)inputString.Count) * 100));
  66.  
  67.  
  68.  
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment