Advertisement
grubcho

Array Histogram

Jul 3rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 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. //You will be given a string array on the console (single line, space-separated). Your task is to make statistics about the elements of //the array. Find out how many times each word occurs in the array. After which, sort the result by the count of occurrences in //descending order and print statistics about every word in the following format:
  7. //{word} -> {count} times ({percentage:F2}%)
  8.  
  9. namespace Array_histogram
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string[] arr = Console.ReadLine().Split(' ').ToArray();
  16.             List<string> words = new List<string>();
  17.             List<int> occurrences = new List<int>();
  18.             for (int i = 0; i < arr.Length; i++)
  19.             {
  20.                 if (!words.Contains(arr[i]))
  21.                 {
  22.                     words.Add(arr[i]);
  23.                     occurrences.Add(1);
  24.                 }
  25.                 else
  26.                 {
  27.                     int wordIndex = words.FindIndex(a => a == arr[i]);
  28.                     occurrences[wordIndex]++;
  29.                 }
  30.             }
  31.             BubbleSort(words, occurrences);
  32.             int sumAllOccurrences = occurrences.Sum();
  33.             double percent = 100.0 / sumAllOccurrences;          
  34.             for (int i = 0; i < occurrences.Count; i++)
  35.             {
  36.                 Console.WriteLine("{0} -> {1} times ({2:F2}%)", words[i], occurrences[i], (double)occurrences[i] * percent);
  37.             }            
  38.         }
  39.         static void BubbleSort( List<string> words, List<int> occur)
  40.         {
  41.             bool swapped;
  42.             do
  43.             {
  44.                 swapped = false;
  45.                 for (int i = 0; i < occur.Count - 1; i++)
  46.                 {
  47.                     if (occur[i] < occur[i+1])
  48.                     {
  49.                         Swap(words, occur, i, i + 1);
  50.                         swapped = true;
  51.                     }
  52.                 }
  53.  
  54.             } while (swapped);
  55.         }
  56.         static void Swap(List<string> words, List<int> occur, int i1, int i2)
  57.         {
  58.             string tempWords = words[i1];
  59.             int tempOccur = occur[i1];
  60.             words[i1] = words[i2];
  61.             occur[i1] = occur[i2];
  62.             words[i2] = tempWords;
  63.             occur[i2] = tempOccur;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement