DyNaMiXx7

Untitled

Feb 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.12 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _11ArrayManipulator
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] arrayNumbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  11.             string command = string.Empty;
  12.  
  13.             while ((command = Console.ReadLine()) != "end")
  14.             {
  15.                 string[] tokens = command.Split(" ");
  16.                 switch (command)
  17.                 {
  18.                     case "max even":
  19.                         MaxEven(arrayNumbers);
  20.                         break;
  21.                     case "max odd":
  22.                         MaxOdd(arrayNumbers);
  23.                         break;
  24.                     case "min even":
  25.                         MinEven(arrayNumbers);
  26.                         break;
  27.                     case "min odd":
  28.                         MinOdd(arrayNumbers);
  29.                         break;
  30.                 }
  31.  
  32.                 if (command.StartsWith("first") && command.EndsWith("even"))
  33.                 {
  34.                     FirstEven(int.Parse(tokens[1]), arrayNumbers);
  35.                 }
  36.                 else if (command.StartsWith("first") && command.EndsWith("odd"))
  37.                 {
  38.                     FirstOdd(int.Parse(tokens[1]), arrayNumbers);
  39.                 }
  40.                 else if (command.StartsWith("last") && command.EndsWith("even"))
  41.                 {
  42.                     LastEven(int.Parse(tokens[1]), arrayNumbers);
  43.                 }
  44.                 else if (command.StartsWith("last") && command.EndsWith("odd"))
  45.                 {
  46.                     LastOdd(int.Parse(tokens[1]), arrayNumbers);
  47.                 }
  48.                 else if (command.StartsWith("exchange"))
  49.                 {
  50.                     Exchange(arrayNumbers, int.Parse(tokens[1]));
  51.                 }
  52.             }
  53.  
  54.             Console.WriteLine($"[{string.Join(", ", arrayNumbers)}]");
  55.         }
  56.  
  57.         private static void Exchange(int[] arrayNumbers, int index)
  58.         {
  59.             if (index > arrayNumbers.Length)
  60.             {
  61.                 Console.WriteLine("Invalid index");
  62.             }
  63.             int[] resultArray = new int [arrayNumbers.Length]; // create new array
  64.  
  65.             for (int i = 0, j = index + 1; j < arrayNumbers.Length; i++, j++) // loop until filling the first part of the array [4, 5]
  66.             {
  67.                 resultArray[i] = arrayNumbers[j];
  68.             }
  69.  
  70.             for (int i = index, j = 0; i < arrayNumbers.Length; i++, j++) // loop until filling the second part of the array [1, 2, 3]
  71.             {
  72.                 resultArray[i] = arrayNumbers[j];
  73.             }
  74.  
  75.             Console.WriteLine($"[{string.Join(", ", resultArray)}]");
  76.         }
  77.  
  78.         private static void MaxEven(int[] arrayNumbers)
  79.         {
  80.             int index = 0;
  81.             int maxValue = int.MinValue;
  82.             bool isMaxEvenExists = false;
  83.  
  84.             for (int i = 0; i < arrayNumbers.Length; i++)
  85.             {
  86.                 if (arrayNumbers[i] % 2 == 0 && arrayNumbers[i] >= maxValue)
  87.                 {
  88.                     index = i;
  89.                     maxValue = arrayNumbers[i];
  90.                     isMaxEvenExists = true;
  91.                 }
  92.             }
  93.  
  94.             if (isMaxEvenExists)
  95.             {
  96.                 Console.WriteLine(index);
  97.             }
  98.             else
  99.             {
  100.                 Console.WriteLine("No matches");
  101.             }
  102.         }
  103.  
  104.         private static void MaxOdd(int[] arrayNumbers)
  105.         {
  106.             int index = 0;
  107.             int maxValue = int.MinValue;
  108.             bool isMaxOddExists = false;
  109.  
  110.             for (int i = 0; i < arrayNumbers.Length; i++)
  111.             {
  112.                 if (arrayNumbers[i] % 2 != 0 && arrayNumbers[i] >= maxValue)
  113.                 {
  114.                     index = i;
  115.                     maxValue = arrayNumbers[i];
  116.                     isMaxOddExists = true;
  117.                 }
  118.             }
  119.  
  120.             if (isMaxOddExists)
  121.             {
  122.                 Console.WriteLine(index);
  123.             }
  124.             else
  125.             {
  126.                 Console.WriteLine("No matches");
  127.             }
  128.         }
  129.  
  130.         private static void MinEven(int[] arrayNumbers)
  131.         {
  132.             int index = 0;
  133.             int minValue = int.MaxValue;
  134.             bool isMinEvenExists = false;
  135.  
  136.             for (int i = 0; i < arrayNumbers.Length; i++)
  137.             {
  138.                 if (arrayNumbers[i] % 2 == 0 && arrayNumbers[i] <= minValue)
  139.                 {
  140.                     index = i;
  141.                     minValue = arrayNumbers[i];
  142.                     isMinEvenExists = true;
  143.                 }
  144.             }
  145.  
  146.             if (isMinEvenExists)
  147.             {
  148.                 Console.WriteLine(index);
  149.             }
  150.             else
  151.             {
  152.                 Console.WriteLine("No matches");
  153.             }
  154.         }
  155.  
  156.         private static void MinOdd(int[] arrayNumbers)
  157.         {
  158.             int index = 0;
  159.             int minValue = int.MaxValue;
  160.             bool isMinOddExists = false;
  161.  
  162.             for (int i = 0; i < arrayNumbers.Length; i++)
  163.             {
  164.                 if (arrayNumbers[i] % 2 != 0 && arrayNumbers[i] <= minValue)
  165.                 {
  166.                     index = i;
  167.                     minValue = arrayNumbers[i];
  168.                     isMinOddExists = true;
  169.                 }
  170.             }
  171.  
  172.             if (isMinOddExists)
  173.             {
  174.                 Console.WriteLine(index);
  175.             }
  176.             else
  177.             {
  178.                 Console.WriteLine("No matches");
  179.             }
  180.         }
  181.  
  182.         private static void FirstEven(int count, int[] arrayNumbers)
  183.         {
  184.             int counterOfEvenElements = 0;
  185.  
  186.             if (count > arrayNumbers.Length)
  187.             {
  188.                 Console.WriteLine("Invalid count");
  189.             }
  190.  
  191.             int[] resultArray = new int[count];
  192.             for (int i = 0; i < arrayNumbers.Length; i++)
  193.             {
  194.                 if (arrayNumbers[i] % 2 == 0)
  195.                 {
  196.                     counterOfEvenElements++;
  197.                     resultArray[i] += arrayNumbers[i];
  198.                     if (counterOfEvenElements == count)
  199.                     {
  200.                         break;
  201.                     }
  202.                 }
  203.             }
  204.  
  205.             if (counterOfEvenElements == 0)
  206.             {
  207.                 Console.WriteLine("[]");
  208.             }
  209.             else
  210.             {
  211.                 Console.WriteLine($"[{string.Join(", ", resultArray)}]");
  212.             }
  213.         }
  214.  
  215.         private static void FirstOdd(int count, int[] arrayNumbers)
  216.         {
  217.             int counterOfOddElements = 0;
  218.  
  219.             if (count > arrayNumbers.Length)
  220.             {
  221.                 Console.WriteLine("Invalid count");
  222.             }
  223.  
  224.             int[] resultArray = new int[count];
  225.             for (int i = 0; i < arrayNumbers.Length; i++)
  226.             {
  227.                 if (arrayNumbers[i] % 2 != 0)
  228.                 {
  229.                     counterOfOddElements++;
  230.                     resultArray[i] += arrayNumbers[i];
  231.                     if (counterOfOddElements == count)
  232.                     {
  233.                         break;
  234.                     }
  235.                 }
  236.             }
  237.  
  238.             if (counterOfOddElements == 0)
  239.             {
  240.                 Console.WriteLine("[]");
  241.             }
  242.             else
  243.             {
  244.                 Console.WriteLine($"[{string.Join(", ", resultArray)}]");
  245.             }
  246.         }
  247.  
  248.         private static void LastEven(int count, int[] arrayNumbers)
  249.         {
  250.             int counterOfEvenElements = 0;
  251.  
  252.             if (count > arrayNumbers.Length)
  253.             {
  254.                 Console.WriteLine("Invalid count");
  255.             }
  256.  
  257.             int[] resultArray = new int[count];
  258.             for (int i = arrayNumbers.Length - 1; i >= 0; i--)
  259.             {
  260.                 if (arrayNumbers[i] % 2 == 0)
  261.                 {
  262.                     resultArray[i] += arrayNumbers[i];
  263.                     counterOfEvenElements++;
  264.                 }
  265.             }
  266.  
  267.             if (counterOfEvenElements == 0)
  268.             {
  269.                 Console.WriteLine("[]");
  270.             }
  271.             else
  272.             {
  273.                 Console.WriteLine($"[{string.Join(", ", resultArray)}]");
  274.             }
  275.         }
  276.  
  277.         private static void LastOdd(int count, int[] arrayNumbers)
  278.         {
  279.             int counterOfOddElements = 0;
  280.  
  281.             if (count > arrayNumbers.Length)
  282.             {
  283.                 Console.WriteLine("Invalid count");
  284.             }
  285.  
  286.             int[] resultArray = new int[count];
  287.             for (int i = arrayNumbers.Length - 1; i >= 0; i--)
  288.             {
  289.                 if (arrayNumbers[i] % 2 != 0)
  290.                 {
  291.                     resultArray[i] += arrayNumbers[i];
  292.                     counterOfOddElements++;
  293.                 }
  294.             }
  295.  
  296.             if (counterOfOddElements == 0)
  297.             {
  298.                 Console.WriteLine("[]");
  299.             }
  300.             else
  301.             {
  302.                 Console.WriteLine($"[{string.Join(", ", resultArray)}]");
  303.             }
  304.         }
  305.     }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment