Advertisement
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 ExamPrep4
- {
- class ArrayManipulator
- {
- static void Main(string[] args)
- {
- List<int> myArray = Console.ReadLine().Split().Select(int.Parse).Where(x=> x>=0 && x<=1000).ToList();
- while (myArray.Count<1 || myArray.Count>50)
- {
- myArray = Console.ReadLine().Split().Select(int.Parse).Where(x => x >= 0 && x <= 1000).ToList();
- }
- while (true)
- {
- string input = Console.ReadLine();
- if (input == "end")
- {
- break;
- }
- string[] commands = input.Split();
- if (commands[0] == "exchange")
- {
- int index = int.Parse(commands[1]);
- if (index > myArray.Count-1 || index<0)
- {
- Console.WriteLine("Invalid index");
- }
- else
- {
- myArray = ExchangeNums(index, myArray);
- }
- }
- else if (commands[0] == "max")
- {
- string evenOrOdd = commands[1];
- int index = MaxEvenOdd(evenOrOdd, myArray);
- if (index >= 0 )
- {
- Console.WriteLine(index);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else if (commands[0] == "min")
- {
- string evenOrOdd = commands[1];
- int index = MinEvenOdd(evenOrOdd, myArray);
- if (index >= 0)
- {
- Console.WriteLine(index);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else if (commands[0] == "first")
- {
- int count = int.Parse(commands[1]);
- string evenOrOdd = commands[2];
- while (count <= 0)
- {
- input = Console.ReadLine();
- }
- if (count > myArray.Count )
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- var tempList = FirstEvenOdd(count, evenOrOdd, myArray);
- if (tempList.Count > 0)
- {
- Console.WriteLine($"[{string.Join(", ", tempList)}]");
- }
- else
- {
- Console.WriteLine("[]");
- }
- }
- }
- else if (commands[0] == "last")
- {
- int count = int.Parse(commands[1]);
- string evenOrOdd = commands[2];
- while (count <= 0)
- {
- input = Console.ReadLine();
- }
- if (count > myArray.Count)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- var tempList = LastEvenOdd(count, evenOrOdd, myArray);
- if (tempList.Count >0)
- {
- Console.WriteLine($"[{string.Join(", ", tempList)}]");
- }
- else
- {
- Console.WriteLine("[]");
- }
- }
- }
- }
- Console.WriteLine($"[{string.Join(", ", myArray)}]");
- }
- private static List<int> LastEvenOdd(int count, string evenOrOdd, List<int> myArray)
- {
- if (evenOrOdd == "even")
- {
- myArray = myArray.Where(x => x % 2 == 0).ToList();
- var temp = myArray.Skip(myArray.Count - count).Take(count).ToList();
- return temp;
- }
- else if (evenOrOdd == "odd")
- {
- myArray = myArray.Where(x => x % 2 != 0).ToList();
- var temp = myArray.Skip(myArray.Count - count).Take(count).ToList();
- return temp;
- }
- return new List<int>();
- }
- private static List<int> FirstEvenOdd(int count, string evenOrOdd, List<int> myArray)
- {
- if (evenOrOdd == "even")
- {
- myArray = myArray.Where(x => x % 2 == 0).ToList();
- var temp = myArray.Take(count).ToList();
- return temp;
- }
- else if (evenOrOdd == "odd")
- {
- myArray = myArray.Where(x => x % 2 != 0).ToList();
- var temp = myArray.Take(count).ToList();
- return temp;
- }
- return new List<int>();
- }
- private static int MinEvenOdd(string evenOrOdd, List<int> myArray)
- {
- if (evenOrOdd == "even")
- {
- List<int> evenList = myArray.Where(x => x % 2 == 0).ToList();
- if (evenList.Count > 0)
- {
- int best = evenList.Min();
- int index = myArray.IndexOf(best);
- if (index >= 0)
- {
- return index;
- }
- }
- }
- else if (evenOrOdd == "odd")
- {
- List<int> oddList = myArray.Where(x => x % 2 != 0).ToList();
- if (oddList.Count > 0)
- {
- int best = oddList.Min();
- int index = myArray.IndexOf(best);
- if (index >= 0)
- {
- return index;
- }
- }
- }
- return -1;
- }
- private static int MaxEvenOdd(string evenOrOdd, List<int> myArray)
- {
- if (evenOrOdd == "even")
- {
- List<int> evenList = myArray.Where(x => x % 2 == 0).ToList();
- if (evenList.Count > 0)
- {
- int best = evenList.Max();
- int index = myArray.IndexOf(best);
- if (index >= 0)
- {
- return index;
- }
- }
- }
- else if (evenOrOdd == "odd")
- {
- List<int> oddList = myArray.Where(x => x % 2 != 0).ToList();
- if (oddList.Count > 0 )
- {
- int best = oddList.Max();
- int index = myArray.IndexOf(best);
- if (index >= 0)
- {
- return index;
- }
- }
- }
- return -1;
- }
- private static List<int> ExchangeNums(int index, List<int> myArray)
- {
- List<int> listAfterExchange = new List<int>();
- listAfterExchange.AddRange(myArray.Skip(index + 1));
- listAfterExchange.AddRange(myArray.Take(index + 1));
- return listAfterExchange;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement