emilia98

Array Manipulator

Aug 12th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.ArrayManipulator
  6. {
  7.     class ArrayManipulator
  8.     {
  9.         static void Main()
  10.         {
  11.             int[] array = Console.ReadLine().Split(new[] { " " },
  12.                                              StringSplitOptions.RemoveEmptyEntries).
  13.                                              Select(int.Parse).ToArray();
  14.             string input = Console.ReadLine();
  15.  
  16.             int leng = array.Length;
  17.  
  18.             while (input != "end")
  19.             {
  20.                 string[] command = input.Split(new[] { " "},
  21.                                                StringSplitOptions.RemoveEmptyEntries);
  22.  
  23.                 if(command[0] == "exchange")
  24.                 {
  25.                     int index = int.Parse(command[1]);
  26.                     if(index >= leng || index < 0)
  27.                     {
  28.                         Console.WriteLine("Invalid index");
  29.                     }
  30.                     else
  31.                     {
  32.                         array = ExchangeArray(array, index);
  33.                     }
  34.                 }
  35.                 else if (command[0] == "max")
  36.                 {
  37.                     string type = command[1];
  38.  
  39.                     int max = ReturnMaxNumberOfType(array, type);
  40.  
  41.                     if(max == -1)
  42.                     {
  43.                         Console.WriteLine("No matches");
  44.                     }
  45.                     else
  46.                     {
  47.                         Console.WriteLine("{0}", max);
  48.                     }                  
  49.                 }
  50.                 else if (command[0] == "min")
  51.                 {
  52.                     string type = command[1];
  53.  
  54.                     int min = ReturnMinNumberOfType(array, type);
  55.  
  56.                     if (min == -1)
  57.                     {
  58.                         Console.WriteLine("No matches");
  59.                     }
  60.                     else
  61.                     {
  62.                         Console.WriteLine("{0}", min);
  63.                     }
  64.                 }
  65.                 else if (command[0] == "first")
  66.                 {
  67.                     int count = int.Parse(command[1]);
  68.                     string type = command[2];
  69.  
  70.                     if(count > leng)
  71.                     {
  72.                         Console.WriteLine("Invalid count");
  73.                     }
  74.                     else
  75.                     {
  76.                         GetFirstCountNumbersOfType(array, count, type);
  77.                     }
  78.                 }
  79.                 else if (command[0] == "last")
  80.                 {
  81.                     int count = int.Parse(command[1]);
  82.                     string type = command[2];
  83.  
  84.                     if (count > leng)
  85.                     {
  86.                         Console.WriteLine("Invalid count");
  87.                     }
  88.                     else
  89.                     {
  90.                         GetLastCountNumbersOfType(array, count, type);
  91.                     }
  92.                 }
  93.                 input = Console.ReadLine();
  94.             }
  95.  
  96.             Console.WriteLine("[{0}]", String.Join(", ", array));
  97.         }
  98.  
  99.         static int[] ExchangeArray(int[] array, int index)
  100.         {
  101.             var newList = new List<int>();
  102.  
  103.             for (int pos = index + 1; pos < array.Length; pos++)
  104.             {
  105.                 newList.Add(array[pos]);
  106.             }
  107.  
  108.             for (int pos = 0; pos < index + 1; pos++)
  109.             {
  110.                 newList.Add(array[pos]);
  111.             }
  112.            
  113.             return newList.ToArray();
  114.         }
  115.  
  116.         static int ReturnMaxNumberOfType(int[] array, string type)
  117.         {
  118.             int maxEven = int.MinValue;
  119.             int maxOdd = int.MinValue;
  120.             int indexMaxEven = 0;
  121.             int indexMaxOdd = 0;
  122.  
  123.             for (int index = 0; index < array.Length; index++)
  124.             {
  125.                 int number = array[index];
  126.  
  127.                 if(number % 2 == 0)
  128.                 {
  129.                     if(number >= maxEven)
  130.                     {
  131.                         maxEven = number;
  132.                         indexMaxEven = index;
  133.                     }
  134.                 }
  135.                 else
  136.                 {
  137.                     if (number >= maxOdd)
  138.                     {
  139.                         maxOdd = number;
  140.                         indexMaxOdd = index;
  141.                     }
  142.                 }
  143.             }
  144.            
  145.             if(type == "even")
  146.             {
  147.                 if(maxEven == int.MinValue)
  148.                 {
  149.                     return -1;
  150.                 }
  151.                 return indexMaxEven;
  152.             }
  153.             else
  154.             {
  155.                 if (maxOdd == int.MinValue)
  156.                 {
  157.                     return -1;
  158.                 }
  159.                 return indexMaxOdd;
  160.             }        
  161.         }
  162.  
  163.         static int ReturnMinNumberOfType(int[] array, string type)
  164.         {
  165.             int minEven = int.MaxValue;
  166.             int minOdd = int.MaxValue;
  167.             int indexMinEven = 0;
  168.             int indexMinOdd = 0;
  169.  
  170.             for (int index = 0; index < array.Length; index++)
  171.             {
  172.                 int number = array[index];
  173.  
  174.                 if (number % 2 == 0)
  175.                 {
  176.                     if (number <= minEven)
  177.                     {
  178.                         minEven = number;
  179.                         indexMinEven = index;
  180.                     }
  181.                 }
  182.                 else
  183.                 {
  184.                     if (number <= minOdd)
  185.                     {
  186.                         minOdd = number;
  187.                         indexMinOdd = index;
  188.                     }
  189.                 }
  190.             }
  191.  
  192.             if (type == "even")
  193.             {
  194.                 if (minEven == int.MaxValue)
  195.                 {
  196.                     return -1;
  197.                 }
  198.                 return indexMinEven;
  199.             }
  200.             else
  201.             {
  202.                 if (minOdd == int.MaxValue)
  203.                 {
  204.                     return -1;
  205.                 }
  206.                 return indexMinOdd;
  207.             }
  208.         }
  209.  
  210.         static void GetFirstCountNumbersOfType(int[] array, int count,string type)
  211.         {
  212.             if(type == "even")
  213.             {
  214.                 int[] arrayOfEvens = GetGroupedElementsByType(array).Item2;
  215.  
  216.                 if(arrayOfEvens.Length == 0)
  217.                 {
  218.                     Console.WriteLine("[]");
  219.                 }
  220.                 else
  221.                 {
  222.                     if(count >= arrayOfEvens.Length)
  223.                     {
  224.                         Console.WriteLine("[{0}]", String.Join(", ",arrayOfEvens));
  225.                     }
  226.                     else
  227.                     {
  228.                         Console.WriteLine("[{0}]", String.Join(", ", arrayOfEvens.Take(count)));
  229.                     }
  230.                 }
  231.             }
  232.             else
  233.             {
  234.                 int[] arrayOfOdds = GetGroupedElementsByType(array).Item1;
  235.  
  236.                 if (arrayOfOdds.Length == 0)
  237.                 {
  238.                     Console.WriteLine("[]");
  239.                 }
  240.                 else
  241.                 {
  242.                     if (count >= arrayOfOdds.Length)
  243.                     {
  244.                         Console.WriteLine("[{0}]", String.Join(", ", arrayOfOdds));
  245.                     }
  246.                     else
  247.                     {
  248.                         Console.WriteLine("[{0}]", String.Join(", ", arrayOfOdds.Take(count)));
  249.                     }
  250.                 }
  251.             }
  252.         }
  253.  
  254.         static void GetLastCountNumbersOfType(int[] array, int count, string type)
  255.         {
  256.             if (type == "even")
  257.             {
  258.                 int[] arrayOfEvens = GetGroupedElementsByType(array).Item2;
  259.  
  260.                 if (arrayOfEvens.Length == 0)
  261.                 {
  262.                     Console.WriteLine("[]");
  263.                 }
  264.                 else
  265.                 {
  266.                     if (count >= arrayOfEvens.Length)
  267.                     {
  268.                         Console.WriteLine("[{0}]", String.Join(", ", arrayOfEvens));
  269.                     }
  270.                     else
  271.                     {
  272.                         Console.WriteLine("[{0}]",
  273.                             String.Join(", ", arrayOfEvens.Skip(arrayOfEvens.Length - count).Take(count)));
  274.                     }
  275.                 }
  276.             }
  277.             else
  278.             {
  279.                 int[] arrayOfOdds = GetGroupedElementsByType(array).Item1;
  280.  
  281.                 if (arrayOfOdds.Length == 0)
  282.                 {
  283.                     Console.WriteLine("[]");
  284.                 }
  285.                 else
  286.                 {
  287.                     if (count >= arrayOfOdds.Length)
  288.                     {
  289.                         Console.WriteLine("[{0}]", String.Join(", ", arrayOfOdds));
  290.                     }
  291.                     else
  292.                     {
  293.                         Console.WriteLine("[{0}]",
  294.                             String.Join(", ", arrayOfOdds.Skip(arrayOfOdds.Length - count).Take(count)));                      
  295.                     }
  296.                 }
  297.             }
  298.         }
  299.  
  300.         static Tuple<int[],int[]> GetGroupedElementsByType(int[] array)
  301.         {
  302.             var oddElements = new List<int>();
  303.             var evenElements = new List<int>();
  304.             for (int index = 0; index < array.Length; index++)
  305.             {
  306.                 int number = array[index];
  307.  
  308.                 if(number % 2 == 1)
  309.                 {
  310.                     oddElements.Add(number);
  311.                 }
  312.                 else
  313.                 {
  314.                     evenElements.Add(number);
  315.                 }
  316.             }
  317.             return new Tuple<int[],int[]>(oddElements.ToArray(), evenElements.ToArray());
  318.         }
  319.     }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment