Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace ArrayManipulator
- {
- class Program
- {
- static void ExchangeIndex(int[] inputArr, int index)
- {
- int[] newArr = new int[inputArr.Length];
- int[] cutInputArrFirst = new int[index + 1];
- int[] cutInputArrSecond = new int[inputArr.Length - index - 1];
- int counter = 0;
- for (int n = 0; n < inputArr.Length; n++)
- {
- if (n <= index)
- {
- cutInputArrFirst[n] = inputArr[n];
- }
- else
- {
- cutInputArrSecond[counter] = inputArr[n];
- counter++;
- }
- }
- Array.ConstrainedCopy(cutInputArrFirst, 0, newArr, newArr.Length - index - 1, cutInputArrFirst.Length);
- Array.ConstrainedCopy(cutInputArrSecond, 0, newArr, 0, cutInputArrSecond.Length);
- //Console.WriteLine("[" + string.Join(", ", newArr) + "]");
- Array.Copy(newArr, inputArr, newArr.Length);
- }
- static void MaxEvenOdd(int[] arr, string evenOrOdd)
- {
- int maxValue = int.MinValue;
- int highestIndex = -1;
- for (int n = 0; n < arr.Length; n++)
- {
- if (arr[n] > maxValue)
- {
- if (evenOrOdd == "even" && arr[n] % 2 == 0)
- {
- maxValue = arr[n];
- highestIndex = n;
- }
- else if (evenOrOdd == "odd" && arr[n] % 2 != 0)
- {
- maxValue = arr[n];
- highestIndex = n;
- }
- }
- }
- if (highestIndex == -1)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(highestIndex);
- }
- }
- static void MinEvenOdd(int[] arr, string evenOrOdd)
- {
- int minValue = int.MaxValue;
- int highesRightmosttIndex = -1;
- for (int n = arr.Length - 1; n >= 0; n--)
- {
- if (arr[n] < minValue)
- {
- if (evenOrOdd == "even" && arr[n] % 2 == 0)
- {
- minValue = arr[n];
- highesRightmosttIndex = n;
- }
- else if (evenOrOdd == "odd" && arr[n] % 2 != 0)
- {
- minValue = arr[n];
- highesRightmosttIndex = n;
- }
- }
- }
- if (highesRightmosttIndex == -1)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(highesRightmosttIndex);
- }
- }
- static void PrintDefinedNumberOfEvenOrOddFirst(int[] arr, int number, string evenOrOdd)
- {
- if (number > arr.Length)
- {
- Console.WriteLine("Invalid count");
- return;
- }
- int counterEven = 0;
- int counterOdd = 0;
- for (int n = 0; n < arr.Length; n++)
- {
- if (arr[n] % 2 == 0)
- {
- counterEven++;
- }
- else
- {
- counterOdd++;
- }
- }
- if ((counterOdd == 0 && evenOrOdd == "odd") || (counterEven == 0 && evenOrOdd == "even"))
- {
- Console.WriteLine("[]");
- return;
- }
- if (counterEven < number && evenOrOdd == "even")
- {
- number = counterEven;
- }
- if (counterOdd < number && evenOrOdd == "odd")
- {
- number = counterOdd;
- }
- int[] arrOdd = new int[counterOdd];
- int[] arrEven = new int[counterEven];
- counterEven = 0;
- counterOdd = 0;
- for (int m = 0; m < arr.Length; m++)
- {
- if (arr[m] % 2 == 0)
- {
- arrEven[counterEven] = arr[m];
- counterEven++;
- }
- else
- {
- arrOdd[counterOdd] = arr[m];
- counterOdd++;
- }
- }
- for (int r = 0; r < number; r++)
- {
- if (r == 0)
- {
- Console.Write("[");
- }
- if (evenOrOdd == "even")
- {
- Console.Write(arrEven[r] + ", ");
- }
- else if (evenOrOdd == "odd")
- {
- Console.Write(arrOdd[r]);
- if (r != number - 1)
- {
- Console.Write(", ");
- }
- }
- if (r == number - 1)
- {
- Console.Write("]");
- }
- }
- Console.WriteLine();
- }
- static void PrintDefinedNumberOfEvenOrOddLast(int[] arr, int number, string evenOrOdd)
- {
- if (number > arr.Length)
- {
- Console.WriteLine("Invalid count");
- return;
- }
- int counterEven = 0;
- int counterOdd = 0;
- for (int n = 0; n < arr.Length; n++)
- {
- if (arr[n] % 2 == 0)
- {
- counterEven++;
- }
- else
- {
- counterOdd++;
- }
- }
- if ((counterOdd == 0 && evenOrOdd == "odd") || (counterEven == 0 && evenOrOdd == "even"))
- {
- Console.WriteLine("[]");
- return;
- }
- if (counterEven < number && evenOrOdd == "even")
- {
- number = counterEven;
- }
- if (counterOdd < number && evenOrOdd == "odd")
- {
- number = counterOdd;
- }
- int[] arrOdd = new int[counterOdd];
- int[] arrEven = new int[counterEven];
- counterEven = 0;
- counterOdd = 0;
- for (int m = arr.Length - 1; m >= 0; m--)
- {
- if (arr[m] % 2 == 0)
- {
- arrEven[counterEven] = arr[m];
- counterEven++;
- }
- else
- {
- arrOdd[counterOdd] = arr[m];
- counterOdd++;
- }
- }
- Array.Reverse(arrEven);
- Array.Reverse(arrOdd);
- for (int r = 0; r < number; r++)
- {
- if (r == 0)
- {
- Console.Write("[");
- }
- if (evenOrOdd == "even")
- {
- Console.Write(arrEven[r] + ", ");
- }
- else if (evenOrOdd == "odd")
- {
- Console.Write(arrOdd[r]);
- if (r != number - 1)
- Console.Write(", ");
- }
- if (r == number - 1)
- {
- Console.Write("]");
- }
- }
- Console.WriteLine();
- }
- static void Main(string[] args)
- {
- int[] inputArr = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToArray();
- while (true)
- {
- string action = Console.ReadLine();
- if (action == "end")
- {
- Console.WriteLine("[" + string.Join(", ", inputArr) + "]");
- break;
- }
- else
- {
- if (action.Contains("exchange") == true)
- {
- string[] actionAsArr = action.Split();
- if (int.Parse(actionAsArr[1]) > inputArr.Length)
- {
- Console.WriteLine("Invalid index");
- }
- else
- {
- ExchangeIndex(inputArr, int.Parse(actionAsArr[1]));
- }
- }
- else if (action == "max even")
- {
- MaxEvenOdd(inputArr, "even");
- }
- else if (action == "max odd")
- {
- MaxEvenOdd(inputArr, "odd");
- }
- else if (action == "min even")
- {
- MinEvenOdd(inputArr, "even");
- }
- else if (action == "min odd")
- {
- MinEvenOdd(inputArr, "odd");
- }
- else if (action.Contains("first") == true)
- {
- string[] actionAsArr = action.Split();
- PrintDefinedNumberOfEvenOrOddFirst(inputArr, int.Parse(actionAsArr[1]), actionAsArr[2]);
- }
- else if (action.Contains("last") == true)
- {
- string[] actionAsArr = action.Split();
- PrintDefinedNumberOfEvenOrOddLast(inputArr, int.Parse(actionAsArr[1]), actionAsArr[2]);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement