Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ArrayManipulator
- {
- class ArrayManipulator
- {
- static List<int> array;
- static void Main(string[] args)
- {
- array = Console.ReadLine().Split().Select(int.Parse).ToList();
- string inputLine = Console.ReadLine().ToLower();
- while (!inputLine.Equals("end"))
- {
- string[] commandInfo = inputLine.Split().ToArray();
- if (commandInfo[0].Equals("exchange"))
- {
- int index = int.Parse(commandInfo[1]);
- if (index < 0 || index >= array.Count)
- {
- Console.WriteLine("Invalid index");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- int[] subSequence = array.TakeWhile((x, i) => i <= index).ToArray();
- array.RemoveRange(0, index + 1);
- array.InsertRange(array.Count, subSequence);
- }
- else if (commandInfo[0].Equals("max") && commandInfo[1].Equals("even"))
- {
- int maxEven = 0;
- int index = 0;
- try
- {
- maxEven = array.Where(x => x % 2 == 0).Max();
- index = array.LastIndexOf(maxEven);
- }
- catch (Exception)
- {
- Console.WriteLine("No matches");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- Console.WriteLine(index);
- }
- else if (commandInfo[0].Equals("max") && commandInfo[1].Equals("odd"))
- {
- int maxOdd = 0;
- int index = 0;
- try
- {
- maxOdd = array.Where(x => x % 2 != 0).Max();
- index = array.LastIndexOf(maxOdd);
- }
- catch (Exception)
- {
- Console.WriteLine("No matches");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- Console.WriteLine(index);
- }
- else if (commandInfo[0].Equals("min") && commandInfo[1].Equals("even"))
- {
- int minEven = 0;
- int index = 0;
- try
- {
- minEven = array.Where(x => x % 2 == 0).Min();
- index = array.LastIndexOf(minEven);
- }
- catch (Exception)
- {
- Console.WriteLine("No matches");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- Console.WriteLine(index);
- }
- else if (commandInfo[0].Equals("min") && commandInfo[1].Equals("odd"))
- {
- int minOdd = 0;
- int index = 0;
- try
- {
- minOdd = array.Where(x => x % 2 != 0).Min();
- index = array.LastIndexOf(minOdd);
- }
- catch (Exception)
- {
- Console.WriteLine("No matches");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- Console.WriteLine(index);
- }
- else if (commandInfo[0].Equals("first") && commandInfo[2].Equals("even"))
- {
- int count = int.Parse(commandInfo[1]);
- if (count > array.Count)
- {
- Console.WriteLine("Invalid count");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- List<int> firstEven = new List<int>();
- foreach (int num in array)
- {
- if (firstEven.Count == count)
- {
- break;
- }
- if (num % 2 == 0)
- {
- firstEven.Add(num);
- }
- }
- Console.WriteLine($"[{string.Join(", ", firstEven)}]");
- }
- else if (commandInfo[0].Equals("first") && commandInfo[2].Equals("odd"))
- {
- int count = int.Parse(commandInfo[1]);
- if (count > array.Count)
- {
- Console.WriteLine("Invalid count");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- List<int> firstOdd = new List<int>();
- foreach (int num in array)
- {
- if (firstOdd.Count == count)
- {
- break;
- }
- if (num % 2 != 0)
- {
- firstOdd.Add(num);
- }
- }
- Console.WriteLine($"[{string.Join(", ", firstOdd)}]");
- }
- else if (commandInfo[0].Equals("last") && commandInfo[2].Equals("even"))
- {
- int count = int.Parse(commandInfo[1]);
- if (count > array.Count)
- {
- Console.WriteLine("Invalid count");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- List<int> lastEven = new List<int>();
- for (int currElement = array.Count - 1; currElement >= 0; currElement--)
- {
- if (lastEven.Count == count)
- {
- break;
- }
- if (array[currElement] % 2 == 0)
- {
- lastEven.Add(array[currElement]);
- }
- }
- Console.WriteLine($"[{string.Join(", ", lastEven)}]");
- }
- else if (commandInfo[0].Equals("last") && commandInfo[2].Equals("odd"))
- {
- int count = int.Parse(commandInfo[1]);
- if (count > array.Count)
- {
- Console.WriteLine("Invalid count");
- inputLine = Console.ReadLine().ToLower();
- continue;
- }
- List<int> lastOdd = new List<int>();
- for (int currElement = array.Count - 1; currElement >= 0; currElement--)
- {
- if (lastOdd.Count == count)
- {
- break;
- }
- if (array[currElement] % 2 != 0)
- {
- lastOdd.Add(array[currElement]);
- }
- }
- Console.WriteLine($"[{string.Join(", ", lastOdd)}]");
- }
- inputLine = Console.ReadLine().ToLower();
- }
- Console.WriteLine($"[{string.Join(", ", array)}]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment