Advertisement
Guest User

11. Array Manipulator 30/100

a guest
Feb 13th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.77 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _11._Array_Manipulator
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray(); //it works
  11.             string input = Console.ReadLine();
  12.             //int[] tempArray = new int[array.Length];
  13.  
  14.             while (input != "end")
  15.             {
  16.                 string[] command = input.Split().ToArray();
  17.                 if (command[0] == "exchange") // it works
  18.                 {
  19.                     int delimer = int.Parse(command[1].ToString());
  20.                     if (delimer < 0 || delimer > array.Length - 1)
  21.                     {
  22.                         Console.WriteLine("Invalid index");
  23.                     }
  24.                     else
  25.                     {
  26.                         array = Exchange(array, delimer);
  27.                     }
  28.                 }
  29.                 else if (command[0] == "max")
  30.                 {
  31.                     string evenOrOdd = command[1].ToString();
  32.                     if (evenOrOdd == "even")
  33.                     {
  34.                         if (IsEvenInArray(array))
  35.                         {
  36.                             int maxIndex = MaxEvenIndex(array, command);
  37.                             Console.WriteLine(maxIndex);
  38.                         }
  39.                         else
  40.                         {
  41.                             Console.WriteLine("No matches");
  42.                         }
  43.                     }      // ДО ТУК ДОБРЕ
  44.                     else if (evenOrOdd == "odd")
  45.                     {
  46.                         if (IsOddInArray(array))
  47.                         {
  48.                             int maxIndex = MaxOddIndex(array, command);
  49.                             Console.WriteLine(maxIndex);
  50.                         }
  51.                         else
  52.                         {
  53.                             Console.WriteLine("No matches");
  54.                         }
  55.                     }
  56.                 }
  57.                 else if (command[0] == "min")
  58.                 {
  59.                     string evenOrOddMin = command[1]; //it works
  60.                     if (evenOrOddMin == "even")
  61.                     {
  62.                         if (IsEvenInArray(array))
  63.                         {
  64.                             int minIndex = MinEvenIndex(array, command);
  65.                             Console.WriteLine(minIndex);
  66.                         }
  67.                         else
  68.                         {
  69.                             Console.WriteLine("No matches");
  70.                         }
  71.                     }
  72.                     else if (evenOrOddMin == "odd")
  73.                     {
  74.                         if (IsOddInArray(array))
  75.                         {
  76.                             int minIndex = MinOddIndex(array, command);
  77.                             Console.WriteLine(minIndex);
  78.                         }
  79.                         else
  80.                         {
  81.                             Console.WriteLine("No matches");
  82.                         }
  83.                     }
  84.                 } // ДО ТУК РАБОТИ
  85.                 else if (command[0] == "first")
  86.                 {
  87.                     int counter = int.Parse(command[1].ToString());
  88.                     if (counter > array.Length)
  89.                     {
  90.                         Console.WriteLine("Invalid count");
  91.                     }
  92.                     else
  93.                     {
  94.                         if (command[2] == "even")
  95.                         {
  96.                             if (IsEvenInArray(array) == false)
  97.                             {
  98.                                 Console.WriteLine($"[]");
  99.                             }
  100.                             else
  101.                             {
  102.                                 FirstEvenElements(array, counter);
  103.                             }
  104.                         }
  105.                         else if (command[2] == "odd")
  106.                         {
  107.                             if (IsOddInArray(array) == false)
  108.                             {
  109.                                 Console.WriteLine($"[]");
  110.                             }
  111.                             else
  112.                             {
  113.                                 FirstOddElements(array, counter);
  114.                             }
  115.                         }
  116.                     }
  117.                 }
  118.                 else if (command[0] == "last")
  119.                 {
  120.                     int counter = int.Parse(command[1].ToString());
  121.                     if (counter > array.Length)
  122.                     {
  123.                         Console.WriteLine("Invalid count");
  124.                     }
  125.                     else
  126.                     {
  127.                         if (command[2] == "even")
  128.                         {
  129.                             if (IsEvenInArray(array) == false)
  130.                             {
  131.                                 Console.WriteLine($"[]");
  132.                             }
  133.                             else
  134.                             {
  135.                                 LastEvenElements(array, counter);
  136.                             }
  137.                         }
  138.                         else if (command[2] == "odd")
  139.                         {
  140.                             if (IsOddInArray(array) == false)
  141.                             {
  142.                                 Console.WriteLine($"[]");
  143.                             }
  144.                             else
  145.                             {
  146.                                 LastOddElements(array, counter);
  147.                             }
  148.                         }
  149.                     }
  150.                 }
  151.                 input = Console.ReadLine();
  152.             }
  153.             Console.WriteLine($"[{string.Join(", ", array)}]");
  154.         }
  155.  
  156.         static int[] Exchange(int[] array, int delimer)
  157.         {
  158.             array = (array.Skip(delimer + 1)).Concat(array.Take(delimer + 1)).ToArray();
  159.             return array;
  160.         }
  161.         static bool IsEvenInArray(int[] array)
  162.         {
  163.             bool isEvenInArray = false;
  164.  
  165.             for (int i = 0; i < array.Length; i++)
  166.             {
  167.                 if (array[i] % 2 == 0)
  168.                 {
  169.                     isEvenInArray = true;
  170.                 }
  171.             }
  172.             return isEvenInArray;
  173.         }
  174.         static bool IsOddInArray(int[] array)
  175.         {
  176.             bool isOddInArray = false;
  177.  
  178.             for (int i = 0; i < array.Length; i++)
  179.             {
  180.                 if (array[i] % 2 != 0)
  181.                 {
  182.                     isOddInArray = true;
  183.                 }
  184.             }
  185.             return isOddInArray;
  186.         }
  187.         static int MaxEvenIndex(int[] array, string[] command)
  188.         {
  189.  
  190.             int maxEvenNumber = int.MinValue;
  191.             int maxEvenNumberIndex = 0;
  192.  
  193.             for (int i = 0; i < array.Length; i++)
  194.             {
  195.                 if (array[i] % 2 == 0)
  196.                 {
  197.                     if (array[i] > maxEvenNumber)
  198.                     {
  199.                         maxEvenNumber = array[i];
  200.                         if (i > maxEvenNumberIndex)
  201.                         {
  202.                             maxEvenNumberIndex = i;
  203.                         }
  204.                     }
  205.                 }
  206.  
  207.             }
  208.             return maxEvenNumberIndex;
  209.         }
  210.         static int MaxOddIndex(int[] array, string[] command)
  211.         {
  212.             int maxOddNumber = int.MinValue;
  213.             int maxNumberIndex = 0;
  214.  
  215.             for (int i = 0; i < array.Length; i++)
  216.             {
  217.                 if (array[i] % 2 != 0)
  218.                 {
  219.                     if (array[i] > maxOddNumber)
  220.                     {
  221.                         maxOddNumber = array[i];
  222.                         if (i > maxNumberIndex)
  223.                         {
  224.                             maxNumberIndex = i;
  225.                         }
  226.                     }
  227.                 }
  228.             }
  229.             return maxNumberIndex;
  230.         }
  231.         static int MinOddIndex(int[] array, string[] command)
  232.         {
  233.             int minOddNumber = int.MaxValue;
  234.             int minNumberIndex = 0;
  235.  
  236.             for (int i = 0; i < array.Length; i++)
  237.             {
  238.                 if (array[i] % 2 != 0)
  239.                 {
  240.                     if (array[i] < minOddNumber)
  241.                     {
  242.                         minOddNumber = array[i];
  243.                         if (i > minNumberIndex)
  244.                         {
  245.                             minNumberIndex = i;
  246.                         }
  247.                     }
  248.                 }
  249.             }
  250.             return minNumberIndex;
  251.         }
  252.         static int MinEvenIndex(int[] array, string[] command)
  253.         {
  254.             int minEvenNumber = int.MaxValue;
  255.             int minNumberIndex = 0;
  256.  
  257.             for (int i = 0; i < array.Length; i++)
  258.             {
  259.                 if (array[i] % 2 == 0)
  260.                 {
  261.                     if (array[i] < minEvenNumber)
  262.                     {
  263.                         minEvenNumber = array[i];
  264.                         if (i > minNumberIndex)
  265.                         {
  266.                             minNumberIndex = i;
  267.                         }
  268.                     }
  269.                 }
  270.             }
  271.             return minNumberIndex;
  272.         }
  273.         static void FirstEvenElements (int[] array, int counter)
  274.         {
  275.             string[] result = new string[array.Length];
  276.             int currentCount = 0;
  277.  
  278.                 for (int i = 0; i < array.Length; i++)
  279.                 {
  280.                     if (array[i] % 2 == 0 && counter > currentCount)
  281.                     {
  282.                         currentCount++;
  283.                         result[i] = array[i].ToString();
  284.                     }
  285.                 }
  286.  
  287.             result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
  288.             Console.WriteLine($"[{string.Join(", ", result)}]");
  289.         }
  290.         static void FirstOddElements(int[] array, int counter)
  291.         {
  292.             string[] result = new string[array.Length];
  293.             int currentCount = 0;
  294.  
  295.             for (int i = 0; i < array.Length; i++)
  296.             {
  297.                 if (array[i] % 2 != 0 && counter > currentCount)
  298.                 {
  299.                     currentCount++;
  300.                     result[i] = array[i].ToString();
  301.                 }
  302.             }
  303.  
  304.             result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
  305.             Console.WriteLine($"[{string.Join(", ", result)}]");
  306.         }
  307.         static void LastEvenElements(int[] array, int counter)
  308.         {
  309.             array = array.Reverse().ToArray();
  310.             string[] result = new string[array.Length];
  311.             int currentCount = 0;
  312.  
  313.             for (int i = 0; i < array.Length; i++)
  314.             {
  315.                 if (array[i] % 2 == 0 && counter > currentCount)
  316.                 {
  317.                     currentCount++;
  318.                     result[i] = array[i].ToString();
  319.                 }
  320.             }
  321.  
  322.             result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
  323.             Console.WriteLine($"[{string.Join(", ", result)}]");
  324.         }
  325.         static void LastOddElements(int[] array, int counter)
  326.         {
  327.             array = array.Reverse().ToArray();
  328.             string[] result = new string[array.Length];
  329.             int currentCount = 0;
  330.  
  331.             for (int i = 0; i < array.Length; i++)
  332.             {
  333.                 if (array[i] % 2 != 0 && counter > currentCount)
  334.                 {
  335.                     currentCount++;
  336.                     result[i] = array[i].ToString();
  337.                 }
  338.             }
  339.  
  340.             result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
  341.             Console.WriteLine($"[{string.Join(", ", result)}]");
  342.         }
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement