Tushlekov

Array Manipulator

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