yanass

Array Manipulator

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