Advertisement
Guest User

Untitled

a guest
May 23rd, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.33 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace P11.ArrayManipulator
  5. {
  6.     internal class 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[] currentCommand = Console.ReadLine()
  16.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries);
  17.  
  18.             while (currentCommand[0] != "end")
  19.             {
  20.                 if (currentCommand[0] == "exchange")
  21.                 {
  22.                     int exchangeIndex = int.Parse(currentCommand[1]);
  23.  
  24.                     if (exchangeIndex < 0 || exchangeIndex >= initialArray.Length)
  25.                     {
  26.                         Console.WriteLine("Invalid index");
  27.  
  28.                         currentCommand = Console.ReadLine()
  29.                             .Split(' ', StringSplitOptions.RemoveEmptyEntries);
  30.                         continue;
  31.                     }
  32.  
  33.                     if (exchangeIndex == initialArray.Length - 1)
  34.                     {
  35.                         currentCommand = Console.ReadLine()
  36.                             .Split(' ', StringSplitOptions.RemoveEmptyEntries);
  37.                         continue;
  38.                     }
  39.  
  40.                     initialArray = ExchangeIndex(initialArray, exchangeIndex);
  41.                 }
  42.                 else if (currentCommand[0] == "max" || currentCommand[0] == "min")
  43.                 {
  44.                     FindIndexOfMaxOrMinEvenOrOddNumber(initialArray, DetermineIfEvenOrOddNumber(currentCommand[1]), currentCommand[0]);
  45.                 }
  46.                 else if (currentCommand[0] == "first" || currentCommand[0] == "last")
  47.                 {
  48.                     int countOfNumbers = int.Parse(currentCommand[1]);
  49.  
  50.                     if (countOfNumbers > initialArray.Length)
  51.                     {
  52.                         Console.WriteLine("Invalid count");
  53.  
  54.                         currentCommand = Console.ReadLine()
  55.                             .Split(' ', StringSplitOptions.RemoveEmptyEntries);
  56.                         continue;
  57.                     }
  58.  
  59.                     FindFirstOrLastNEvenOrOddNumbers(initialArray, currentCommand[0], countOfNumbers, DetermineIfEvenOrOddNumber(currentCommand[2]));
  60.                 }
  61.  
  62.                 currentCommand = Console.ReadLine()
  63.                     .Split(' ', StringSplitOptions.RemoveEmptyEntries);
  64.             }
  65.  
  66.             Console.WriteLine($"[{string.Join(", ", initialArray)}]");
  67.         }
  68.  
  69.         static int[] ExchangeIndex(int[] currentArray, int exchangeIndex)
  70.         {
  71.             int[] exchangedArray = new int[currentArray.Length];
  72.             int counter = 0;
  73.  
  74.             for (int currentIndex = 0; currentIndex < currentArray.Length; currentIndex++)
  75.             {
  76.                 if (currentIndex < currentArray.Length - exchangeIndex - 1)
  77.                 {
  78.                     exchangedArray[currentIndex] = currentArray[exchangeIndex + 1 + currentIndex];
  79.                     continue;
  80.                 }
  81.  
  82.                 exchangedArray[currentIndex] = currentArray[counter];
  83.                 counter++;
  84.             }
  85.  
  86.             return exchangedArray;
  87.         }
  88.  
  89.         static int DetermineIfEvenOrOddNumber(string evenOrOdd)
  90.         {
  91.             return evenOrOdd == "even" ? 0 : 1;
  92.         }
  93.  
  94.         static void FindIndexOfMaxOrMinEvenOrOddNumber(int[] currentArray, int evenOrOdd, string maxOrMin)
  95.         {
  96.             int maxNumber = int.MinValue;
  97.             int minNumber = int.MaxValue;
  98.             int indexOfSelectedNumber = -1;
  99.  
  100.             for (int currentIndex = 0; currentIndex < currentArray.Length; currentIndex++)
  101.             {
  102.                 if (maxOrMin == "max")
  103.                 {
  104.                     if (currentArray[currentIndex] % 2 == evenOrOdd && currentArray[currentIndex] >= maxNumber)
  105.                     {
  106.                         maxNumber = currentArray[currentIndex];
  107.                         indexOfSelectedNumber = currentIndex;
  108.                     }
  109.                 }
  110.                 else if (maxOrMin == "min")
  111.                 {
  112.                     if (currentArray[currentIndex] % 2 == evenOrOdd && currentArray[currentIndex] <= minNumber)
  113.                     {
  114.                         minNumber = currentArray[currentIndex];
  115.                         indexOfSelectedNumber = currentIndex;
  116.                     }
  117.                 }
  118.             }
  119.  
  120.             string output = indexOfSelectedNumber == -1 ? "No matches" : indexOfSelectedNumber.ToString();
  121.  
  122.             Console.WriteLine(output);
  123.             }
  124.  
  125.         static void FindFirstOrLastNEvenOrOddNumbers(int[] currentArray, string firstOrLast, int countOfNumbers, int evenOrOdd)
  126.         {
  127.             int counter = 0;
  128.             string firstOrLastNSelectedNumbers = " ";
  129.  
  130.             if (firstOrLast == "first")
  131.             {
  132.                 for (int currentIndex = 0; currentIndex < currentArray.Length; currentIndex++)
  133.                 {
  134.                     if (currentArray[currentIndex] % 2 == evenOrOdd)
  135.                     {
  136.                         counter++;
  137.  
  138.                         firstOrLastNSelectedNumbers += currentArray[currentIndex] + " ";
  139.                     }
  140.  
  141.                     if (counter == countOfNumbers)
  142.                     {
  143.                         break;
  144.                     }
  145.                 }
  146.             }
  147.             else
  148.             {
  149.                 for (int currentIndex = currentArray.Length - 1; currentIndex >= 0; currentIndex--)
  150.                 {
  151.                     if (currentArray[currentIndex] % 2 == evenOrOdd)
  152.                     {
  153.                         counter++;
  154.  
  155.                         firstOrLastNSelectedNumbers = currentArray[currentIndex] + " " + firstOrLastNSelectedNumbers;
  156.                     }
  157.  
  158.                     if (counter == countOfNumbers)
  159.                     {
  160.                         break;
  161.                     }
  162.                 }
  163.             }
  164.  
  165.             int[] firstNEvenNumbersToAray = firstOrLastNSelectedNumbers
  166.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  167.                 .Select(int.Parse)
  168.                 .ToArray();
  169.  
  170.             Console.WriteLine($"[{string.Join(", ", firstNEvenNumbersToAray)}]");
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement