Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _11._Array_Manipulator
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] inputArray = Console.ReadLine().Split().Select(int.Parse).ToArray();
- string commandOrEnd = Console.ReadLine();
- while (commandOrEnd != "end")
- {
- string[] commandArray = commandOrEnd.Split();
- if (commandArray[0] == "exchange")
- {
- int exchangeIndex = int.Parse(commandArray[1]);
- inputArray = GetExchangedArray(exchangeIndex, inputArray);
- }
- else if (commandArray[0] == "max")
- {
- if (commandArray[1] == "even")
- {
- PrintMaxEven(inputArray);
- }
- else
- {
- PrintMaxOdd(inputArray);
- }
- }
- else if (commandArray[0] == "min")
- {
- if (commandArray[1] == "even")
- {
- PrintMinEven(inputArray);
- }
- else
- {
- PrintMinOdd(inputArray);
- }
- }
- else if (commandArray[0] == "first")
- {
- string count = commandArray[1];
- int firstCount = int.Parse(count);
- if (commandArray[2] == "even")
- {
- PrintFirstEven(inputArray,firstCount);
- }
- else if (commandArray[2] == "odd")
- {
- PrintFirstOdd(inputArray,firstCount);
- }
- }
- else if (commandArray[0] == "last")
- {
- string count = commandArray[1];
- int lastCount = int.Parse(count);
- if (commandArray[2] == "even")
- {
- PrintLastEven(inputArray,lastCount);
- }
- else if (commandArray[2] == "odd")
- {
- PrintLastOdd(inputArray, lastCount);
- }
- }
- commandOrEnd = Console.ReadLine();
- }
- PrintFinalArray(inputArray);
- }
- static int[] GetExchangedArray(int exchangeIndex, int[] inputArray)
- {
- string arrayBeforeExchange = string.Empty;
- if (exchangeIndex < 0 || inputArray.Length - 1 < exchangeIndex)
- {
- Console.WriteLine("Invalid index");
- return inputArray;
- }
- else
- {
- for (int i = exchangeIndex + 1; i < inputArray.Length; i++)
- {
- arrayBeforeExchange += inputArray[i] + " ";
- }
- for (int i = 0; i < exchangeIndex + 1; i++)
- {
- if (i == exchangeIndex)
- {
- arrayBeforeExchange += inputArray[i];
- }
- else
- {
- arrayBeforeExchange += inputArray[i] + " ";
- }
- }
- string[] arrayAfterExchange = arrayBeforeExchange.Split();
- inputArray = arrayAfterExchange.Select(int.Parse).ToArray();
- return inputArray;
- }
- }
- static void PrintMaxEven(int[] inputArray)
- {
- int bestMaxEven = int.MinValue;
- int bestIndex = 0;
- bool isMaxEven = false;
- for (int i = 0; i < inputArray.Length; i++)
- {
- int currentNum = inputArray[i];
- if (currentNum % 2 == 0)
- {
- if (bestMaxEven < currentNum)
- {
- bestMaxEven = currentNum;
- bestIndex = i;
- isMaxEven = true;
- }
- }
- }
- if (isMaxEven == false)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(bestIndex);
- }
- }
- static void PrintMaxOdd(int[] inputArray)
- {
- int bestMaxOdd = int.MinValue;
- int bestIndex = 0;
- bool isMaxOdd = false;
- for (int i = 0; i < inputArray.Length; i++)
- {
- int currentNum = inputArray[i];
- if (currentNum % 2 != 0)
- {
- if (bestMaxOdd < currentNum)
- {
- bestMaxOdd = currentNum;
- bestIndex = i;
- isMaxOdd = true;
- }
- }
- }
- if (isMaxOdd == false)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(bestIndex);
- }
- }
- static void PrintMinEven(int[] inputArray)
- {
- int bestMinEven = int.MaxValue;
- int bestIndex = 0;
- bool isMinEven = false;
- for (int i = 0; i < inputArray.Length; i++)
- {
- int currentNum = inputArray[i];
- if (currentNum % 2 == 0)
- {
- if (currentNum < bestMinEven)
- {
- bestMinEven = currentNum;
- bestIndex = i;
- isMinEven = true;
- }
- }
- }
- if (isMinEven == false)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(bestIndex);
- }
- }
- static void PrintMinOdd(int[] inputArray)
- {
- int bestMinOdd = int.MaxValue;
- int bestIndex = 0;
- bool isMinOdd = false;
- for (int i = 0; i < inputArray.Length; i++)
- {
- int currentNum = inputArray[i];
- if (currentNum % 2 != 0)
- {
- if (currentNum < bestMinOdd)
- {
- bestMinOdd = currentNum;
- bestIndex = i;
- isMinOdd = true;
- }
- }
- }
- if (isMinOdd == false)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(bestIndex);
- }
- }
- static void PrintFirstEven(int[] inputArray, int firstCount)
- {
- if (firstCount < 0 || inputArray.Length < firstCount)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- string result = string.Empty;
- for (int i = 0; i < firstCount; i++)
- {
- int currentNumber = inputArray[i];
- if (currentNumber % 2 == 0)
- {
- result += currentNumber;
- }
- }
- Console.Write("[");
- for (int i = 0; i < result.Length; i++)
- {
- if (i == result.Length - 1)
- {
- Console.Write(result[i]);
- }
- else
- {
- Console.Write(result[i] + "," + " ");
- }
- }
- Console.Write("]");
- Console.WriteLine();
- }
- }
- static void PrintFirstOdd(int[]inputArray,int firstCount)
- {
- if (firstCount < 0 || inputArray.Length < firstCount)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- string result = string.Empty;
- for (int i = 0; i < firstCount; i++)
- {
- int currentNumber = inputArray[i];
- if (currentNumber % 2 != 0)
- {
- result += currentNumber;
- }
- }
- Console.Write("[");
- for (int i = 0; i < result.Length; i++)
- {
- if (i == result.Length - 1)
- {
- Console.Write(result[i]);
- }
- else
- {
- Console.Write(result[i] + "," + " ");
- }
- }
- Console.Write("]");
- Console.WriteLine();
- }
- }
- static void PrintLastEven(int[]inputArray,int lastCount)
- {
- if (lastCount < 0 || inputArray.Length < lastCount)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- string result = string.Empty;
- for (int i = inputArray.Length - lastCount; i < inputArray.Length; i++)
- {
- int currentNumber = inputArray[i];
- if (currentNumber % 2 == 0)
- {
- result += currentNumber;
- }
- }
- Console.Write("[");
- for (int i = 0; i < result.Length; i++)
- {
- if (i == result.Length - 1)
- {
- Console.Write(result[i]);
- }
- else
- {
- Console.Write(result[i] + "," + " ");
- }
- }
- Console.Write("]");
- Console.WriteLine();
- }
- }
- static void PrintLastOdd(int[]inputArray,int lastCount)
- {
- if (lastCount < 0 || inputArray.Length < lastCount)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- string result = string.Empty;
- for (int i = inputArray.Length - lastCount; i < inputArray.Length; i++)
- {
- int currentNumber = inputArray[i];
- if (currentNumber % 2 != 0)
- {
- result += currentNumber;
- }
- }
- Console.Write("[");
- for (int i = 0; i < result.Length; i++)
- {
- if (i == result.Length - 1)
- {
- Console.Write(result[i]);
- }
- else
- {
- Console.Write(result[i] + "," + " ");
- }
- }
- Console.Write("]");
- Console.WriteLine();
- }
- }
- static void PrintFinalArray(int[] arrayAfterExchange)
- {
- string result = string.Empty;
- Console.Write("[");
- for (int i = 0; i < arrayAfterExchange.Length; i++)
- {
- if (i == arrayAfterExchange.Length - 1)
- {
- result += arrayAfterExchange[i];
- }
- else
- {
- result += arrayAfterExchange[i] + "," + " ";
- }
- }
- Console.Write(result);
- Console.Write("]");
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement