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()
- {
- int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray(); //it works
- string input = Console.ReadLine();
- //int[] tempArray = new int[array.Length];
- while (input != "end")
- {
- string[] command = input.Split().ToArray();
- if (command[0] == "exchange") // it works
- {
- int delimer = int.Parse(command[1].ToString());
- if (delimer < 0 || delimer > array.Length - 1)
- {
- Console.WriteLine("Invalid index");
- }
- else
- {
- array = Exchange(array, delimer);
- }
- }
- else if (command[0] == "max")
- {
- string evenOrOdd = command[1].ToString();
- if (evenOrOdd == "even")
- {
- if (IsEvenInArray(array))
- {
- int maxIndex = MaxEvenIndex(array, command);
- Console.WriteLine(maxIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- } // ДО ТУК ДОБРЕ
- else if (evenOrOdd == "odd")
- {
- if (IsOddInArray(array))
- {
- int maxIndex = MaxOddIndex(array, command);
- Console.WriteLine(maxIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- }
- else if (command[0] == "min")
- {
- string evenOrOddMin = command[1]; //it works
- if (evenOrOddMin == "even")
- {
- if (IsEvenInArray(array))
- {
- int minIndex = MinEvenIndex(array, command);
- Console.WriteLine(minIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else if (evenOrOddMin == "odd")
- {
- if (IsOddInArray(array))
- {
- int minIndex = MinOddIndex(array, command);
- Console.WriteLine(minIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- } // ДО ТУК РАБОТИ
- else if (command[0] == "first")
- {
- int counter = int.Parse(command[1].ToString());
- if (counter > array.Length)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- if (command[2] == "even")
- {
- if (IsEvenInArray(array) == false)
- {
- Console.WriteLine($"[]");
- }
- else
- {
- FirstEvenElements(array, counter);
- }
- }
- else if (command[2] == "odd")
- {
- if (IsOddInArray(array) == false)
- {
- Console.WriteLine($"[]");
- }
- else
- {
- FirstOddElements(array, counter);
- }
- }
- }
- }
- else if (command[0] == "last")
- {
- int counter = int.Parse(command[1].ToString());
- if (counter > array.Length)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- if (command[2] == "even")
- {
- if (IsEvenInArray(array) == false)
- {
- Console.WriteLine($"[]");
- }
- else
- {
- LastEvenElements(array, counter);
- }
- }
- else if (command[2] == "odd")
- {
- if (IsOddInArray(array) == false)
- {
- Console.WriteLine($"[]");
- }
- else
- {
- LastOddElements(array, counter);
- }
- }
- }
- }
- input = Console.ReadLine();
- }
- Console.WriteLine($"[{string.Join(", ", array)}]");
- }
- static int[] Exchange(int[] array, int delimer)
- {
- array = (array.Skip(delimer + 1)).Concat(array.Take(delimer + 1)).ToArray();
- return array;
- }
- static bool IsEvenInArray(int[] array)
- {
- bool isEvenInArray = false;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0)
- {
- isEvenInArray = true;
- }
- }
- return isEvenInArray;
- }
- static bool IsOddInArray(int[] array)
- {
- bool isOddInArray = false;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0)
- {
- isOddInArray = true;
- }
- }
- return isOddInArray;
- }
- static int MaxEvenIndex(int[] array, string[] command)
- {
- int maxEvenNumber = int.MinValue;
- int maxEvenNumberIndex = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0)
- {
- if (array[i] > maxEvenNumber)
- {
- maxEvenNumber = array[i];
- if (i > maxEvenNumberIndex)
- {
- maxEvenNumberIndex = i;
- }
- }
- }
- }
- return maxEvenNumberIndex;
- }
- static int MaxOddIndex(int[] array, string[] command)
- {
- int maxOddNumber = int.MinValue;
- int maxNumberIndex = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0)
- {
- if (array[i] > maxOddNumber)
- {
- maxOddNumber = array[i];
- if (i > maxNumberIndex)
- {
- maxNumberIndex = i;
- }
- }
- }
- }
- return maxNumberIndex;
- }
- static int MinOddIndex(int[] array, string[] command)
- {
- int minOddNumber = int.MaxValue;
- int minNumberIndex = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0)
- {
- if (array[i] < minOddNumber)
- {
- minOddNumber = array[i];
- if (i > minNumberIndex)
- {
- minNumberIndex = i;
- }
- }
- }
- }
- return minNumberIndex;
- }
- static int MinEvenIndex(int[] array, string[] command)
- {
- int minEvenNumber = int.MaxValue;
- int minNumberIndex = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0)
- {
- if (array[i] < minEvenNumber)
- {
- minEvenNumber = array[i];
- if (i > minNumberIndex)
- {
- minNumberIndex = i;
- }
- }
- }
- }
- return minNumberIndex;
- }
- static void FirstEvenElements (int[] array, int counter)
- {
- string[] result = new string[array.Length];
- int currentCount = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0 && counter > currentCount)
- {
- currentCount++;
- result[i] = array[i].ToString();
- }
- }
- result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- static void FirstOddElements(int[] array, int counter)
- {
- string[] result = new string[array.Length];
- int currentCount = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0 && counter > currentCount)
- {
- currentCount++;
- result[i] = array[i].ToString();
- }
- }
- result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- static void LastEvenElements(int[] array, int counter)
- {
- array = array.Reverse().ToArray();
- string[] result = new string[array.Length];
- int currentCount = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0 && counter > currentCount)
- {
- currentCount++;
- result[i] = array[i].ToString();
- }
- }
- result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- static void LastOddElements(int[] array, int counter)
- {
- array = array.Reverse().ToArray();
- string[] result = new string[array.Length];
- int currentCount = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0 && counter > currentCount)
- {
- currentCount++;
- result[i] = array[i].ToString();
- }
- }
- result = result.Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement