alexbancheva

Array Manipulator

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