Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace P11.ArrayManipulator
- {
- internal class ArrayManipulator
- {
- static void Main(string[] args)
- {
- int[] initialArray = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- string[] currentCommand = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries);
- while (currentCommand[0] != "end")
- {
- if (currentCommand[0] == "exchange")
- {
- int exchangeIndex = int.Parse(currentCommand[1]);
- if (exchangeIndex < 0 || exchangeIndex >= initialArray.Length)
- {
- Console.WriteLine("Invalid index");
- currentCommand = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries);
- continue;
- }
- if (exchangeIndex == initialArray.Length - 1)
- {
- currentCommand = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries);
- continue;
- }
- initialArray = ExchangeIndex(initialArray, exchangeIndex);
- }
- else if (currentCommand[0] == "max" || currentCommand[0] == "min")
- {
- FindIndexOfMaxOrMinEvenOrOddNumber(initialArray, DetermineIfEvenOrOddNumber(currentCommand[1]), currentCommand[0]);
- }
- else if (currentCommand[0] == "first" || currentCommand[0] == "last")
- {
- int countOfNumbers = int.Parse(currentCommand[1]);
- if (countOfNumbers > initialArray.Length)
- {
- Console.WriteLine("Invalid count");
- currentCommand = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries);
- continue;
- }
- FindFirstOrLastNEvenOrOddNumbers(initialArray, currentCommand[0], countOfNumbers, DetermineIfEvenOrOddNumber(currentCommand[2]));
- }
- currentCommand = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries);
- }
- Console.WriteLine($"[{string.Join(", ", initialArray)}]");
- }
- static int[] ExchangeIndex(int[] currentArray, int exchangeIndex)
- {
- int[] exchangedArray = new int[currentArray.Length];
- int counter = 0;
- for (int currentIndex = 0; currentIndex < currentArray.Length; currentIndex++)
- {
- if (currentIndex < currentArray.Length - exchangeIndex - 1)
- {
- exchangedArray[currentIndex] = currentArray[exchangeIndex + 1 + currentIndex];
- continue;
- }
- exchangedArray[currentIndex] = currentArray[counter];
- counter++;
- }
- return exchangedArray;
- }
- static int DetermineIfEvenOrOddNumber(string evenOrOdd)
- {
- return evenOrOdd == "even" ? 0 : 1;
- }
- static void FindIndexOfMaxOrMinEvenOrOddNumber(int[] currentArray, int evenOrOdd, string maxOrMin)
- {
- int maxNumber = int.MinValue;
- int minNumber = int.MaxValue;
- int indexOfSelectedNumber = -1;
- for (int currentIndex = 0; currentIndex < currentArray.Length; currentIndex++)
- {
- if (maxOrMin == "max")
- {
- if (currentArray[currentIndex] % 2 == evenOrOdd && currentArray[currentIndex] >= maxNumber)
- {
- maxNumber = currentArray[currentIndex];
- indexOfSelectedNumber = currentIndex;
- }
- }
- else if (maxOrMin == "min")
- {
- if (currentArray[currentIndex] % 2 == evenOrOdd && currentArray[currentIndex] <= minNumber)
- {
- minNumber = currentArray[currentIndex];
- indexOfSelectedNumber = currentIndex;
- }
- }
- }
- string output = indexOfSelectedNumber == -1 ? "No matches" : indexOfSelectedNumber.ToString();
- Console.WriteLine(output);
- }
- static void FindFirstOrLastNEvenOrOddNumbers(int[] currentArray, string firstOrLast, int countOfNumbers, int evenOrOdd)
- {
- int counter = 0;
- string firstOrLastNSelectedNumbers = " ";
- if (firstOrLast == "first")
- {
- for (int currentIndex = 0; currentIndex < currentArray.Length; currentIndex++)
- {
- if (currentArray[currentIndex] % 2 == evenOrOdd)
- {
- counter++;
- firstOrLastNSelectedNumbers += currentArray[currentIndex] + " ";
- }
- if (counter == countOfNumbers)
- {
- break;
- }
- }
- }
- else
- {
- for (int currentIndex = currentArray.Length - 1; currentIndex >= 0; currentIndex--)
- {
- if (currentArray[currentIndex] % 2 == evenOrOdd)
- {
- counter++;
- firstOrLastNSelectedNumbers = currentArray[currentIndex] + " " + firstOrLastNSelectedNumbers;
- }
- if (counter == countOfNumbers)
- {
- break;
- }
- }
- }
- int[] firstNEvenNumbersToAray = firstOrLastNSelectedNumbers
- .Split(' ', StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- Console.WriteLine($"[{string.Join(", ", firstNEvenNumbersToAray)}]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement