Advertisement
Guest User

ArrayManipulator

a guest
Feb 15th, 2019
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.98 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _11_ArrayManipulator
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] initialArray = Console.ReadLine()
  11.                 .Split()
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.            
  15.             string command = Console.ReadLine();
  16.  
  17.             while (command != "end")
  18.             {
  19.                 string[] manipulationCommands = command.Split().ToArray();
  20.  
  21.                 if (manipulationCommands[0] == "exchange")
  22.                 {
  23.                     int index = int.Parse(manipulationCommands[1]);
  24.  
  25.                     if (index < 0 || index > initialArray.Length - 1)
  26.                     {
  27.                         Console.WriteLine("Invalid index");
  28.                     }
  29.                     else
  30.                     {
  31.                         initialArray = Exchange(initialArray, index);
  32.                     }
  33.                 }
  34.  
  35.                 else if (manipulationCommands[0] == "max")
  36.                 {
  37.                     if (manipulationCommands[1] == "even")
  38.                     {
  39.                         int maxEvenIndex = GetMaxEven(initialArray);
  40.                         if (maxEvenIndex != -1)
  41.                         {
  42.                             Console.WriteLine(maxEvenIndex);
  43.                         }
  44.                         else
  45.                         {
  46.                             Console.WriteLine("No matches");
  47.                         }
  48.                     }
  49.  
  50.                     else if (manipulationCommands[1] == "odd")
  51.                     {
  52.                         int maxOddIndex = GetMaxOdd(initialArray);
  53.                         if (maxOddIndex != -1)
  54.                         {
  55.                             Console.WriteLine(maxOddIndex);
  56.                         }
  57.                         else
  58.                         {
  59.                             Console.WriteLine("No matches");
  60.                         }
  61.                     }
  62.                 }
  63.  
  64.                 else if (manipulationCommands[0] == "min")
  65.                 {
  66.                     if (manipulationCommands[1] == "even")
  67.                     {
  68.                         int minEvenIndex = GetMinEven(initialArray);
  69.                         if (minEvenIndex != -1)
  70.                         {
  71.                             Console.WriteLine(minEvenIndex);
  72.                         }
  73.                         else
  74.                         {
  75.                             Console.WriteLine("No matches");
  76.                         }
  77.                     }
  78.  
  79.                     else if (manipulationCommands[1] == "odd")
  80.                     {
  81.                         int minOddIndex = GetMinOdd(initialArray);
  82.                         if (minOddIndex != -1)
  83.                         {
  84.                             Console.WriteLine(minOddIndex);
  85.                         }
  86.                         else
  87.                         {
  88.                             Console.WriteLine("No matches");
  89.                         }
  90.                     }
  91.                 }
  92.  
  93.                 else if (manipulationCommands[0] == "first")
  94.                 {
  95.                     int count = int.Parse(manipulationCommands[1]);
  96.  
  97.                     if (count > initialArray.Length)
  98.                     {
  99.                         Console.WriteLine("Invalid count");
  100.                     }
  101.  
  102.                     else
  103.                     {
  104.                         if (manipulationCommands[2] == "even")
  105.                         {
  106.                             int[] firstEven = GetFirstEven(initialArray, count);
  107.  
  108.                             if (firstEven[0] == 0)
  109.                             {
  110.                                 Console.WriteLine("[]");
  111.                             }
  112.  
  113.                             else
  114.                             {
  115.                                 PrintArray(firstEven);
  116.                             }
  117.                         }
  118.  
  119.                         else if (manipulationCommands[2] == "odd")
  120.                         {
  121.                             int[] firstOdd = GetFirstOdd(initialArray, count);
  122.  
  123.                             if (firstOdd[0] == 0)
  124.                             {
  125.                                 Console.WriteLine("[]");
  126.                             }
  127.  
  128.                             else
  129.                             {
  130.                                 PrintArray(firstOdd);
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.  
  136.                 else if (manipulationCommands[0] == "last")
  137.                 {
  138.                     int count = int.Parse(manipulationCommands[1]);
  139.  
  140.                     if (count > initialArray.Length)
  141.                     {
  142.                         Console.WriteLine("Invalid count");
  143.                     }
  144.  
  145.                     else
  146.                     {
  147.                         if (manipulationCommands[2] == "even")
  148.                         {
  149.                             int[] lastEven = GetLastEven(initialArray, count);
  150.  
  151.                             if (lastEven[lastEven.Length - 1] == 0)
  152.                             {
  153.                                 Console.WriteLine("[]");
  154.                             }
  155.  
  156.                             else
  157.                             {
  158.                                 PrintArray(lastEven);
  159.                             }
  160.                         }
  161.  
  162.                         else if (manipulationCommands[2] == "odd")
  163.                         {
  164.                             int[] lastOdd = GetLastOdd(initialArray, count);
  165.  
  166.                             if (lastOdd[lastOdd.Length - 1] == 0)
  167.                             {
  168.                                 Console.WriteLine("[]");
  169.                             }
  170.  
  171.                             else
  172.                             {
  173.                                 PrintArray(lastOdd);
  174.                             }
  175.                         }
  176.                     }
  177.                 }
  178.                                
  179.                 command = Console.ReadLine();
  180.             }
  181.  
  182.             PrintArray(initialArray);
  183.         }
  184.  
  185.         private static int[] Exchange(int[] initialArray, int index)
  186.         {
  187.             int[] subArr1 = new int[index + 1];
  188.             int[] subArr2 = new int[initialArray.Length - (index + 1)];
  189.             int[] arrExchanged = new int[initialArray.Length];
  190.  
  191.             for (int i = 0; i < subArr1.Length; i++)
  192.             {
  193.                 subArr1[i] = initialArray[i];
  194.             }
  195.  
  196.             for (int j = 0; j < subArr2.Length; j++)
  197.             {
  198.                 subArr2[j] = initialArray[j + index + 1];
  199.             }
  200.  
  201.             for (int k = 0; k < subArr2.Length; k++)
  202.             {
  203.                 arrExchanged[k] = subArr2[k];
  204.             }
  205.  
  206.             for (int l = 0; l < subArr1.Length; l++)
  207.             {
  208.                 arrExchanged[l + subArr2.Length] = subArr1[l];
  209.             }
  210.            
  211.             return arrExchanged;            
  212.         }
  213.  
  214.         private static int GetMaxEven(int[] initialArray)
  215.         {
  216.             int maxEven = 0;
  217.             int maxEvenIndex = -1;
  218.  
  219.             for (int i = 0; i < initialArray.Length; i++)
  220.             {
  221.                 if (initialArray[i] != 0 && initialArray[i] % 2 == 0)
  222.                 {
  223.                     int currentEven = initialArray[i];
  224.                     if (currentEven >= maxEven)
  225.                     {
  226.                         maxEven = currentEven;
  227.                         maxEvenIndex = i;
  228.                     }
  229.                 }
  230.             }
  231.  
  232.             return maxEvenIndex;
  233.         }
  234.  
  235.         private static int GetMaxOdd(int[] initialArray)
  236.         {
  237.             int maxOdd = 0;
  238.             int maxOddIndex = -1;
  239.  
  240.             for (int i = 0; i < initialArray.Length; i++)
  241.             {
  242.                 if (initialArray[i] != 0 && initialArray[i] % 2 != 0)
  243.                 {
  244.                     int currentOdd = initialArray[i];
  245.                     if (currentOdd >= maxOdd)
  246.                     {
  247.                         maxOdd = currentOdd;
  248.                         maxOddIndex = i;
  249.                     }
  250.                 }
  251.             }
  252.  
  253.             return maxOddIndex;
  254.         }
  255.  
  256.         private static int GetMinEven(int[] initialArray)
  257.         {
  258.             int minEven = int.MaxValue;
  259.             int minEvenIndex = -1;
  260.  
  261.             for (int i = 0; i < initialArray.Length; i++)
  262.             {
  263.                 if (initialArray[i] != 0 && initialArray[i] % 2 == 0)
  264.                 {
  265.                     int currentEven = initialArray[i];
  266.                     if (currentEven <= minEven)
  267.                     {
  268.                         minEven = currentEven;
  269.                         minEvenIndex = i;
  270.                     }
  271.                 }
  272.             }
  273.  
  274.             return minEvenIndex;
  275.         }
  276.  
  277.         private static int GetMinOdd(int[] initialArray)
  278.         {
  279.             int minOdd = int.MaxValue;
  280.             int minOddIndex = -1;
  281.  
  282.             for (int i = 0; i < initialArray.Length; i++)
  283.             {
  284.                 if (initialArray[i] != 0 && initialArray[i] % 2 != 0)
  285.                 {
  286.                     int currentOdd = initialArray[i];
  287.                     if (currentOdd <= minOdd)
  288.                     {
  289.                         minOdd = currentOdd;
  290.                         minOddIndex = i;
  291.                     }
  292.                 }
  293.             }
  294.  
  295.             return minOddIndex;
  296.         }
  297.  
  298.         private static int[] GetFirstEven(int[] initialArray, int count)
  299.         {
  300.             int evenCount = 0;
  301.             for (int i = 0; i < initialArray.Length; i++)
  302.             {
  303.                 if (initialArray[i] != 0 && initialArray[i] % 2 == 0)
  304.                 {
  305.                     evenCount++;
  306.                 }
  307.             }
  308.  
  309.             if (count > evenCount && evenCount != 0)
  310.             {
  311.                 count = evenCount;
  312.             }
  313.  
  314.             int[] firstEven = new int[count];
  315.             int j = 0;
  316.  
  317.             for (int i = 0; i < initialArray.Length; i++)
  318.             {
  319.                 if (initialArray[i] != 0 && initialArray[i] % 2 == 0)
  320.                 {
  321.                     if (j < firstEven.Length)
  322.                     {
  323.                         firstEven[j] = initialArray[i];
  324.                         j++;
  325.                     }
  326.                 }
  327.             }
  328.  
  329.             return firstEven;
  330.         }
  331.  
  332.         private static int[] GetFirstOdd(int[] initialArray, int count)
  333.         {
  334.             int oddCount = 0;
  335.             for (int i = 0; i < initialArray.Length; i++)
  336.             {
  337.                 if (initialArray[i] != 0 && initialArray[i] % 2 != 0)
  338.                 {
  339.                     oddCount++;
  340.                 }
  341.             }
  342.  
  343.             if (count > oddCount && oddCount != 0)
  344.             {
  345.                 count = oddCount;
  346.             }
  347.  
  348.             int[] firstOdd = new int[count];
  349.             int j = 0;
  350.  
  351.             for (int i = 0; i < initialArray.Length; i++)
  352.             {
  353.                 if (initialArray[i] != 0 && initialArray[i] % 2 != 0)
  354.                 {
  355.                     if (j < firstOdd.Length)
  356.                     {
  357.                         firstOdd[j] = initialArray[i];
  358.                         j++;
  359.                     }
  360.                 }
  361.             }
  362.  
  363.             return firstOdd;
  364.         }
  365.  
  366.         private static int[] GetLastEven(int[] initialArray, int count)
  367.         {
  368.             int evenCount = 0;
  369.             for (int i = 0; i < initialArray.Length; i++)
  370.             {
  371.                 if (initialArray[i] != 0 && initialArray[i] % 2 == 0)
  372.                 {
  373.                     evenCount++;
  374.                 }
  375.             }
  376.  
  377.             if (count > evenCount && evenCount != 0)
  378.             {
  379.                 count = evenCount;
  380.             }
  381.  
  382.             int[] lastEven = new int[count];
  383.             int j = lastEven.Length - 1;
  384.  
  385.             for (int i = initialArray.Length - 1; i >= 0; i--)
  386.             {            
  387.                 if (initialArray[i] != 0 && initialArray[i] % 2 == 0)
  388.                 {
  389.                     if (j >= 0)
  390.                     {
  391.                         lastEven[j] = initialArray[i];
  392.                         j--;
  393.                     }
  394.                 }
  395.             }
  396.  
  397.             return lastEven;
  398.         }
  399.  
  400.         private static int[] GetLastOdd(int[] initialArray, int count)
  401.         {
  402.             int oddCount = 0;
  403.             for (int i = 0; i < initialArray.Length; i++)
  404.             {
  405.                 if (initialArray[i] != 0 && initialArray[i] % 2 != 0)
  406.                 {
  407.                     oddCount++;
  408.                 }
  409.             }
  410.  
  411.             if (count > oddCount && oddCount != 0)
  412.             {
  413.                 count = oddCount;
  414.             }
  415.  
  416.             int[] lastOdd = new int[count];
  417.             int j = lastOdd.Length - 1;
  418.  
  419.             for (int i = initialArray.Length - 1; i >= 0; i--)
  420.             {
  421.                 if (initialArray[i] != 0 && initialArray[i] % 2 != 0)
  422.                 {
  423.                     if (j >= 0)
  424.                     {
  425.                         lastOdd[j] = initialArray[i];
  426.                         j--;
  427.                     }
  428.                 }
  429.             }
  430.  
  431.             return lastOdd;
  432.         }
  433.  
  434.         private static void PrintArray(int[] arrayToPrint)
  435.         {
  436.             for (int i = 0; i < arrayToPrint.Length; i++)
  437.             {
  438.                 if (i == 0 && arrayToPrint.Length == 1)
  439.                 {
  440.                     Console.WriteLine($"[{arrayToPrint[0]}]");
  441.                 }
  442.                 else if (i == 0 && arrayToPrint.Length > 1)
  443.                 {
  444.                     Console.Write($"[{arrayToPrint[i]}, ");
  445.                 }
  446.                 else if (i == arrayToPrint.Length - 1)
  447.                 {
  448.                     Console.WriteLine($"{arrayToPrint[i]}]");
  449.                 }
  450.                 else
  451.                 {
  452.                     Console.Write(arrayToPrint[i] + ", ");
  453.                 }
  454.             }
  455.         }
  456.     }
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement