Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _02._Array_Manipulator
- {
- class Program
- {
- static void Main()
- {
- var collection = Console.ReadLine().Trim().Split().Select(int.Parse).ToList();
- while (true)
- {
- var input = Console.ReadLine();
- if (input == "end")
- {
- break;
- }
- var command = input.Split();
- if (command[0] == "exchange")
- {
- var index = int.Parse(command[1]);
- if (index < 0 || index > collection.Count - 1)
- {
- Console.WriteLine("Invalid index");
- continue;
- }
- var firstPart = collection.Take(index + 1).ToList();
- collection.RemoveRange(0, index + 1);
- collection.AddRange(firstPart);
- }
- else if (command[0] == "max" && command[1] == "even")
- {
- var collectionHasMaxElement = collection.Where(x => x % 2 == 0).ToList();
- if (collectionHasMaxElement.Count == 0)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- var maxEven = collection.Where(x => x % 2 == 0).Max();
- var indexOfMaxEven = collection.LastIndexOf(maxEven);
- Console.WriteLine(indexOfMaxEven);
- }
- }
- else if (command[0] == "max" && command[1] == "odd")
- {
- var collectionHasMaxElement = collection.Where(x => x % 2 != 0).ToList();
- if (collectionHasMaxElement.Count == 0)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- var maxOdd = collection.Where(x => x % 2 != 0).Max();
- var indexOfMaxOdd = collection.LastIndexOf(maxOdd);
- Console.WriteLine(indexOfMaxOdd);
- }
- }
- else if (command[0] == "min" && command[1] == "even")
- {
- var collectionHasMinElement = collection.Where(x => x % 2 == 0).ToList();
- if (collectionHasMinElement.Count == 0)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- var minEven = collection.Where(x => x % 2 == 0).Min();
- var indexOfMinEven = collection.LastIndexOf(minEven);
- Console.WriteLine(indexOfMinEven);
- }
- }
- else if (command[0] == "min" && command[1] == "odd")
- {
- var collectionHasMinElement = collection.Where(x => x % 2 != 0).ToList();
- if (collectionHasMinElement.Count == 0)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- var minOdd = collection.Where(x => x % 2 != 0).Min();
- var indexOfMinOdd = collection.LastIndexOf(minOdd);
- Console.WriteLine(indexOfMinOdd);
- }
- }
- else if (command[0] == "first" && command[2] == "even")
- {
- var count = int.Parse(command[1]);
- if (count > collection.Count)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- var elemtsToPrint = collection.Where(x => x % 2 == 0).Take(count).ToList();
- Console.WriteLine($"[{string.Join(", ", elemtsToPrint)}]");
- }
- }
- else if (command[0] == "first" && command[2] == "odd")
- {
- var count = int.Parse(command[1]);
- if (count > collection.Count)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- var elemtsToPrint = collection.Where(x => x % 2 != 0).Take(count).ToList();
- Console.WriteLine($"[{string.Join(", ", elemtsToPrint)}]");
- }
- }
- else if (command[0] == "last" && command[2] == "even")
- {
- var count = int.Parse(command[1]);
- if (count > collection.Count)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- var elemtsToPrint = collection.Where(x => x % 2 == 0).TakeLast(count).ToList();
- Console.WriteLine($"[{string.Join(", ", elemtsToPrint)}]");
- }
- }
- else if (command[0] == "last" && command[2] == "odd")
- {
- var count = int.Parse(command[1]);
- if (count > collection.Count)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- var elemtsToPrint = collection.Where(x => x % 2 != 0).TakeLast(count).ToList();
- Console.WriteLine($"[{string.Join(", ", elemtsToPrint)}]");
- }
- }
- }
- Console.WriteLine($"[{string.Join(", ", collection)}]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement