datenshiddd

C# HackerRank, 10 Days of Statistics, Day 1: Quartiles

Jan 6th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Quartiles
  6. {
  7.     class Program
  8.     {
  9.         private static List<int> numbers = new List<int>();
  10.         private static List<int> lowerQuartile = new List<int>();
  11.         private static List<int> upperQuartile = new List<int>();
  12.  
  13.         private static string[] STDIN_Numbers;
  14.  
  15.         private static int numberOfElements = 0;
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             numberOfElements = Convert.ToInt32(Console.ReadLine()); // User input - number of elements
  20.             STDIN_Numbers = Console.ReadLine().Split(' '); // User input - single spaced element list
  21.             PrepareList();
  22.             PrepareQuartiles();
  23.  
  24.             Console.WriteLine($"{IntMedian(lowerQuartile)}");
  25.             Console.WriteLine($"{IntMedian(numbers)}");
  26.             Console.WriteLine($"{IntMedian(upperQuartile)}");
  27.         }
  28.  
  29.         private static void PrepareList() // Prepare main numbers list since user input is string type and list require int type
  30.         {
  31.             int outputNumber;
  32.             for (int i = 0; i < STDIN_Numbers.Length; i++) // Populate numbers list from user input.
  33.             {
  34.                 if(!int.TryParse(STDIN_Numbers[i], out outputNumber)) // Check if input is a number and convert it to int
  35.                 {
  36.                     Console.WriteLine("Error: Input parameter is not an integral number.");
  37.                 }
  38.                 numbers.Add(outputNumber); // Add converted and checked elements to list
  39.             }
  40.  
  41.             numbers.Sort(); // Sort list ascending
  42.         }
  43.  
  44.         private static void PrepareQuartiles() // Split main list into lower and upper quartile lists
  45.         {
  46.             // notEvenSection is used in not even amount of elements or as first element in even amount.
  47.             // evenSection is used only for even amount of elements.
  48.             int notEvenSection = numbers.Count / 2;
  49.             int evenSection = (numbers.Count / 2) - 1;
  50.  
  51.             if (IsEven(numbers.Count)) // Check if element list is even
  52.             {
  53.                 for (int i = 0; i <= evenSection; i++) // Populate lower quartile list
  54.                 {
  55.                     lowerQuartile.Add(numbers[i]);
  56.                 }
  57.                 for (int i = notEvenSection; i < numbers.Count; i++) // Populate upper quartile list
  58.                 {
  59.                     upperQuartile.Add(numbers[i]);
  60.                 }
  61.             }
  62.             else // Run if list in odd
  63.             {
  64.                 for (int i = 0; i < notEvenSection; i++) // Populate lower quartile list
  65.                 {
  66.                     lowerQuartile.Add(numbers[i]);
  67.                 }
  68.                 for (int i = notEvenSection + 1; i < numbers.Count; i++) // Populate upper quartile list
  69.                 {
  70.                     upperQuartile.Add(numbers[i]);
  71.                 }
  72.             }
  73.         }
  74.  
  75.         private static int IntMedian(List<int> _list) // Calculate median in integral list
  76.         {
  77.             int firstSection = _list.Count / 2;
  78.             int secondSection = (_list.Count / 2) - 1;
  79.  
  80.             if (IsEven(_list.Count)) // Return median from even list
  81.             {
  82.                 return (_list.ElementAt(firstSection) + _list.ElementAt(secondSection)) / 2;
  83.             }
  84.             else // Return median from odd list
  85.             {
  86.                 return _list.ElementAt(firstSection);
  87.             }
  88.         }
  89.  
  90.         private static bool IsEven(int _arg) // Check if number is even or odd, return true if even. General helper.
  91.         {
  92.             if (_arg % 2 == 0)
  93.                 return true;
  94.             else
  95.                 return false;
  96.         }
  97.     }
  98. }
Add Comment
Please, Sign In to add comment