Guest User

Untitled

a guest
Jun 13th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.14 KB | None | 0 0
  1. namespace P11_ArrayManipulator
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     class P11_ArrayManipulator
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] initialArray = Console.ReadLine()
  11.                                         .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  12.                                         .Select(int.Parse)
  13.                                         .ToArray();
  14.  
  15.             string[] command = Console.ReadLine().Split();
  16.  
  17.             while (command[0] != "end")
  18.             {
  19.                 string message = string.Empty;
  20.                 switch (command[0])
  21.                 {
  22.                     case "exchange":
  23.                         Exchange(initialArray, command);
  24.                         command = Console.ReadLine().Split();
  25.                         continue;
  26.  
  27.                     case "max":
  28.                         int index = Max(initialArray, command);
  29.                         message = (index >= 0) ? $"{index}" : "No matches";
  30.                         break;
  31.  
  32.                     case "min":
  33.                         index = Min(initialArray, command);
  34.                         message = (index >= 0) ? $"{index}" : "No matches";
  35.                         break;
  36.  
  37.                     case "first":
  38.                         int count = int.Parse(command[1]);
  39.                         if (count > initialArray.Length)
  40.                         {
  41.                             message = "Invalid count";
  42.                             break;
  43.                         }
  44.  
  45.                         string evenOrOdd = command[2];
  46.                         int[] nums = First(initialArray, count, evenOrOdd);
  47.  
  48.                         message = (nums.Length == 0 ? "[]" : $"[{String.Join(", ", nums)}]");
  49.                         break;
  50.  
  51.                     case "last":
  52.                         count = int.Parse(command[1]);
  53.                         if (count > initialArray.Length)
  54.                         {
  55.                             message = "Invalid count";
  56.                             break;
  57.                         }
  58.  
  59.                         evenOrOdd = command[2];
  60.                         nums = Last(initialArray, count, evenOrOdd);
  61.                         message = (nums.Length == 0 ? "[]" : $"[{String.Join(", ", nums)}]");
  62.                         break;                  
  63.                 }
  64.  
  65.                 Console.WriteLine(message);
  66.                 command = Console.ReadLine().Split();
  67.             }
  68.  
  69.             Console.WriteLine($"[{String.Join(", ", initialArray)}]");
  70.         }
  71.  
  72.         public static void Exchange(int[] initialArray, string[] command)
  73.         {
  74.             int index = int.Parse(command[1]);
  75.             if (index < 0 || index >= initialArray.Length)
  76.             {
  77.                 Console.WriteLine("Invalid index");
  78.                 return;
  79.             }
  80.  
  81.             int[] leftSide = new int[index + 1];
  82.             int[] rightSide = new int[initialArray.Length - 1 - index];
  83.             int counter = 0;
  84.  
  85.             for (int i = 0; i < initialArray.Length; i++)
  86.             {
  87.                 if (i < leftSide.Length)
  88.                 {
  89.                     leftSide[i] = initialArray[i];
  90.                 }
  91.                 else
  92.                 {
  93.                     rightSide[counter] = initialArray[i];
  94.                     counter++;
  95.                 }
  96.             }
  97.  
  98.             counter = 0;
  99.             for (int i = 0; i < initialArray.Length; i++)
  100.             {
  101.                 if (i < rightSide.Length)
  102.                 {
  103.                     initialArray[i] = rightSide[i];
  104.                 }
  105.                 else
  106.                 {
  107.                     initialArray[i] = leftSide[counter++];
  108.                 }
  109.             }
  110.         }
  111.  
  112.         public static int Max(int[] initialArray, string[] command)
  113.         {
  114.             int max = int.MinValue;
  115.             int index = -1;
  116.  
  117.             switch (command[1])
  118.             {
  119.                 case "even":
  120.                     for (int i = 0; i < initialArray.Length; i++)
  121.                     {
  122.                         if (initialArray[i] % 2 == 0 && initialArray[i] >= max)
  123.                         {
  124.                             max = initialArray[i];
  125.                             index = i;
  126.                         }
  127.                     }
  128.  
  129.                     break;
  130.  
  131.                 case "odd":
  132.                     for (int i = 0; i < initialArray.Length; i++)
  133.                     {
  134.                         if (initialArray[i] % 2 != 0 && initialArray[i] >= max)
  135.                         {
  136.                             max = initialArray[i];
  137.                             index = i;
  138.                         }
  139.                     }
  140.  
  141.                     break;
  142.             }
  143.  
  144.             return index;
  145.         }
  146.  
  147.         public static int Min(int[] initialArray, string[] command)
  148.         {
  149.             int min = int.MaxValue;
  150.             int index = -1;
  151.             switch (command[1])
  152.             {
  153.                 case "even":
  154.                     for (int i = 0; i < initialArray.Length; i++)
  155.                     {
  156.                         if (initialArray[i] % 2 == 0 && initialArray[i] <= min)
  157.                         {
  158.                             min = initialArray[i];
  159.                             index = i;
  160.                         }
  161.                     }
  162.  
  163.                     break;
  164.  
  165.                 case "odd":
  166.                     for (int i = 0; i < initialArray.Length; i++)
  167.                     {
  168.                         if (initialArray[i] % 2 != 0 && initialArray[i] <= min)
  169.                         {
  170.                             min = initialArray[i];
  171.                             index = i;
  172.                         }
  173.                     }
  174.  
  175.                     break;
  176.             }
  177.  
  178.             return index;
  179.         }
  180.  
  181.         public static int[] First(int[] initialArray, int count, string evenOrOdd)
  182.         {
  183.             string nums = string.Empty;
  184.             switch (evenOrOdd)
  185.             {
  186.                 case "even":
  187.                     foreach (int digit in initialArray)
  188.                     {
  189.                         if (count > 0 && digit % 2 == 0)
  190.                         {
  191.                             nums += $"{digit} ";
  192.                             count--;
  193.                         }
  194.                     }
  195.                     break;
  196.  
  197.                 case "odd":
  198.                     foreach (int digit in initialArray)
  199.                     {
  200.                         if (count > 0 && digit % 2 != 0)
  201.                         {
  202.                             nums += $"{digit} ";
  203.                             count--;
  204.                         }
  205.                     }
  206.                     break;
  207.             }
  208.             nums.TrimEnd();
  209.  
  210.             return nums.Split(' ', StringSplitOptions.RemoveEmptyEntries)
  211.                        .Select(int.Parse)
  212.                        .ToArray();
  213.         }
  214.  
  215.         public static int[] Last(int[] initialArray, int count, string evenOrOdd)
  216.         {
  217.             string nums = string.Empty;
  218.             switch (evenOrOdd)
  219.             {
  220.                 case "even":
  221.                     for (int i = initialArray.Length - 1; i >= 0; i--)
  222.                     {
  223.                         if (count > 0 && initialArray[i] % 2 == 0)
  224.                         {
  225.                             nums += $"{initialArray[i]} ";
  226.                             count--;
  227.                         }
  228.                     }
  229.                     break;
  230.  
  231.                 case "odd":
  232.                     for (int i = initialArray.Length - 1; i >= 0; i--)
  233.                     {
  234.                         if (count > 0 && initialArray[i] % 2 != 0)
  235.                         {
  236.                             nums += $"{initialArray[i]} ";
  237.                             count--;
  238.                         }
  239.                     }
  240.                     break;
  241.             }
  242.             nums.TrimEnd();
  243.  
  244.             return nums.Split(' ', StringSplitOptions.RemoveEmptyEntries)
  245.                        .Select(int.Parse)
  246.                        .ToArray()
  247.                        .Reverse()
  248.                        .ToArray();                      
  249.         }
  250.     }
  251. }
Add Comment
Please, Sign In to add comment