Advertisement
Guest User

ArrayManipulator

a guest
Oct 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.80 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. namespace ArrayManipulator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] mainArray = Console.ReadLine().Split().Select(int.Parse).ToArray();
  10.             while (true)
  11.             {
  12.                 string[] input = Console.ReadLine().Split().ToArray();
  13.                 if (input[0] == "end")
  14.                 {
  15.                     break;
  16.                 }
  17.                 if (input[0] == "exchange")
  18.                 {
  19.                     int exchangeIndex = int.Parse(input[1]);
  20.                     if (exchangeIndex > mainArray.Length - 1 && exchangeIndex < 0)
  21.                     {
  22.                         Console.WriteLine("Invalid index");
  23.                         continue;
  24.                     }
  25.                         Exchange(mainArray, exchangeIndex);
  26.                 }
  27.                 else if (input[0] == "max")
  28.                 {
  29.                     int index = -1;
  30.                     if (input[1] == "even")
  31.                     {
  32.                         index = GetMaxEvenOrOddIndex(mainArray, 0);
  33.                     }
  34.                     else if (input[1] == "odd")
  35.                     {
  36.                         index = GetMaxEvenOrOddIndex(mainArray, 1);
  37.                     }
  38.                     if (index == -1)
  39.                     {
  40.                         Console.WriteLine("No matches");
  41.                         continue;
  42.                     }
  43.                     Console.WriteLine(index);
  44.                 }
  45.                 else if (input[0] == "min")
  46.                 {
  47.                     int index = -1;
  48.                     if (input[1] == "even")
  49.                     {
  50.                         index = GetMinEvenOrOddIndex(mainArray, 0);
  51.                     }
  52.                     else if (input[1] == "odd")
  53.                     {
  54.                         index = GetMinEvenOrOddIndex(mainArray, 1);
  55.                     }
  56.                     if (index == -1)
  57.                     {
  58.                         Console.WriteLine("No matches");
  59.                         continue;
  60.                     }
  61.                     Console.WriteLine(index);
  62.                 }
  63.                 else if (input[0] == "first")
  64.                 {
  65.                     int[] result = new int[0];
  66.                     int count = int.Parse(input[1]);
  67.                     if (count > mainArray.Length)
  68.                     {
  69.                         Console.WriteLine("Invalid count");
  70.                         continue;
  71.                     }
  72.                     if (input[2] == "even")
  73.                     {
  74.                         result = GetFirstEvenOrOddNumber(mainArray, count, 0);
  75.                     }
  76.                     else if (input[2] == "odd")
  77.                     {
  78.                         result = GetFirstEvenOrOddNumber(mainArray, count, 1);
  79.                     }
  80.                     Console.WriteLine($"[{string.Join(", ", result)}]");
  81.                 }
  82.                 else if (input[0] == "last")
  83.                 {
  84.                     int[] result = new int[0];
  85.                     int count = int.Parse(input[1]);
  86.                     if (count>mainArray.Length)
  87.                     {
  88.                         Console.WriteLine("Invalid count");
  89.                         continue;
  90.                     }
  91.                     if (input[2] == "even")
  92.                     {
  93.                         result = GetLastEvenOrOddNumber(mainArray, count, 0);
  94.                     }
  95.                     else if (input[2] == "odd")
  96.                     {
  97.                         result = GetLastEvenOrOddNumber(mainArray, count, 1);
  98.                     }
  99.                     Console.WriteLine($"[{string.Join(", ", result)}]");
  100.                 }
  101.             }
  102.             Console.WriteLine($"[{string.Join(", ",mainArray)}]");
  103.         }
  104.  
  105.         private static int[] GetLastEvenOrOddNumber(int[] mainArray, int count, int divisionResult)
  106.         {
  107.             int[] arrResult = new int[count];
  108.             int curentCount = 0;
  109.             for (int i = mainArray.Length-1; i >= 0; i--)
  110.             {
  111.                 if (mainArray[i] % 2 == divisionResult && curentCount < count)
  112.                 {
  113.                     arrResult[curentCount] = mainArray[i];
  114.                     curentCount++;
  115.                 }
  116.             }
  117.             if (curentCount >= count)
  118.             {
  119.                 return arrResult.Reverse().ToArray();
  120.             }
  121.             else
  122.             {
  123.                 int[] temp = new int[curentCount];
  124.                 Array.Copy(arrResult, temp, curentCount);
  125.                 return temp.Reverse().ToArray();
  126.             }
  127.         }
  128.  
  129.         private static int[] GetFirstEvenOrOddNumber(int[] mainArray, int count, int divisionResult)
  130.         {
  131.             int[] arrResult = new int[count];
  132.             int curentCount = 0;
  133.             for (int i = 0; i < mainArray.Length; i++)
  134.             {
  135.                 if (mainArray[i] % 2 == divisionResult && curentCount < count)
  136.                 {
  137.                     arrResult[curentCount] = mainArray[i];
  138.                     curentCount++;
  139.                 }
  140.             }
  141.             if (curentCount >= count)
  142.             {
  143.                 return arrResult;
  144.             }
  145.             else
  146.             {
  147.                 int[] temp = new int[curentCount];
  148.                 Array.Copy(arrResult,temp,curentCount);
  149.                 return temp;
  150.             }
  151.         }
  152.  
  153.         private static int GetMinEvenOrOddIndex(int[] array, int divisionResult)
  154.         {
  155.             int minNumber = int.MaxValue;
  156.             int index = -1;
  157.             for (int i = 0; i < array.Length; i++)
  158.             {
  159.                 if (array[i] < minNumber && array[i] % 2 == divisionResult)
  160.                 {
  161.                     minNumber = array[i];
  162.                     index = i;
  163.                 }
  164.             }
  165.             return index;
  166.         }
  167.  
  168.         private static int GetMaxEvenOrOddIndex(int[] array, int divisionResult)
  169.         {
  170.             int maxNumber = int.MinValue;
  171.             int index = -1;
  172.             for (int i = 0; i <array.Length; i++)
  173.             {
  174.                 if (array[i] >= maxNumber && array[i] % 2 == divisionResult)
  175.                 {
  176.                     maxNumber = array[i];
  177.                     index = i;
  178.                 }
  179.             }
  180.             return index;
  181.         }
  182.  
  183.         private static void Exchange(int[] mainArray, int exchangeIndex)
  184.         {
  185.             for (int i = 0; i < exchangeIndex + 1; i++)
  186.             {
  187.                 int firstNumber = mainArray[0];
  188.                 for (int a = 0; a < mainArray.Length-1; a++)
  189.                 {
  190.                     mainArray[a] = mainArray[a + 1];
  191.                 }
  192.                 mainArray[mainArray.Length - 1] = firstNumber;
  193.             }
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement