Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- internal class ArrayManipulator
- {
- private static void Main()
- {
- List<int> numbers = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
- var evens = numbers.Where(n => n % 2 == 0);
- var odds = numbers.Where(n => n % 2 == 1);
- int L = numbers.Count;
- while (true)
- {
- string line = Console.ReadLine();
- if (line == "end")
- {
- break;
- }
- string[] commandArgs = line.Split(' ');
- string command = commandArgs[0];
- switch (command)
- {
- case "exchange":
- int index = int.Parse(commandArgs[1]);
- if (Exchange(index, numbers))
- {
- continue;
- }
- evens = numbers.Where(n => n % 2 == 0);
- odds = numbers.Where(n => n % 2 == 1);
- break;
- case "max":
- string type = commandArgs[1];
- PrintMax(type, evens, numbers, odds);
- break;
- case "min":
- type = commandArgs[1];
- PrintMin(type, evens, numbers, odds);
- break;
- case "first":
- int count = int.Parse(commandArgs[1]);
- PrintFirstX(count, numbers, commandArgs, evens, odds);
- break;
- case "last":
- count = int.Parse(commandArgs[1]);
- PrintLastX(count, numbers, commandArgs, evens, odds);
- break;
- }
- }
- Console.WriteLine("[{0}]", string.Join(", ", numbers));
- }
- private static void PrintLastX(int count, List<int> numbers, string[] commandArgs, IEnumerable<int> evens, IEnumerable<int> odds)
- {
- if (count > numbers.Count)
- {
- Console.WriteLine("Invalid count");
- return;
- }
- string type = commandArgs[2];
- if (type == "even")
- {
- int evensCount = evens.Count();
- int finalCount = count > evensCount ? evensCount : count;
- int skipped = evensCount - finalCount;
- var result = evens.Skip(skipped).Take(finalCount);
- Console.WriteLine("[{0}]", string.Join(", ", result));
- }
- else
- {
- int oddsCount = odds.Count();
- int finalCount = count > oddsCount ? oddsCount : count;
- int skipped = oddsCount - finalCount;
- var result = odds.Skip(skipped).Take(finalCount);
- Console.WriteLine("[{0}]", string.Join(", ", result));
- }
- }
- private static void PrintFirstX(int count, List<int> numbers, string[] commandArgs, IEnumerable<int> evens, IEnumerable<int> odds)
- {
- if (count > numbers.Count)
- {
- Console.WriteLine("Invalid count");
- return;
- }
- string type = commandArgs[2];
- if (type == "even")
- {
- var result = evens.Take(count);
- Console.WriteLine("[{0}]", string.Join(", ", result));
- }
- else
- {
- var result = odds.Take(count);
- Console.WriteLine("[{0}]", string.Join(", ", result));
- }
- }
- private static void PrintMin(string type, IEnumerable<int> evens, List<int> numbers, IEnumerable<int> odds)
- {
- if (type == "even")
- {
- if (evens.Any())
- {
- int minEven = evens.Min();
- Console.WriteLine(numbers.LastIndexOf(minEven));
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else
- {
- if (odds.Any())
- {
- int minOdd = odds.Min();
- Console.WriteLine(numbers.LastIndexOf(minOdd));
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- }
- private static void PrintMax(string type, IEnumerable<int> evens, List<int> numbers, IEnumerable<int> odds)
- {
- if (type == "even")
- {
- if (evens.Any())
- {
- int maxEven = evens.Max();
- Console.WriteLine(numbers.LastIndexOf(maxEven));
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else
- {
- if (odds.Any())
- {
- int maxOdd = odds.Max();
- Console.WriteLine(numbers.LastIndexOf(maxOdd));
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- }
- private static bool Exchange(int index, List<int> numbers)
- {
- if (index < 0 || index > numbers.Count - 1)
- {
- Console.WriteLine("Invalid index");
- return true;
- }
- int[] temp = numbers.Take(index + 1).ToArray();
- numbers.RemoveRange(0, index + 1);
- numbers.AddRange(temp);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement