Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ArrayManipulator
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class ArrayManipulator
- {
- public static void Main()
- {
- List<string> collection = Console.ReadLine()
- .Split(new[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries)
- .ToList();
- string command = Console.ReadLine();
- while (command != "end")
- {
- string[] commandArguments = command.Split(new[] { ' ', ',' });
- switch (commandArguments[0])
- {
- case "exchange":
- ExecuteExchangeCommand(collection, commandArguments);
- Console.WriteLine("[{0}]", string.Join(", ", collection));
- break;
- case "max":
- ExecuteMaxCommand(collection, commandArguments);
- break;
- case "min":
- ExecuteMinCommand(collection, commandArguments);
- break;
- case "first":
- ExecuteFirstCommand(collection, commandArguments);
- break;
- case "last":
- ExecuteLastCommand(collection, commandArguments);
- break;
- }
- command = Console.ReadLine();
- }
- }
- private static void ExecuteLastCommand(List<string> collection, string[] commandArguments)
- {
- int count = int.Parse(commandArguments[1]);
- if (commandArguments[2] == "even")
- {
- var collectionToInt = collection.Select(int.Parse).ToList();
- List<int> evenNumbers = new List<int>();
- foreach (var item in collectionToInt)
- {
- if (item % 2 != 0)
- {
- evenNumbers.Remove(item);
- }
- if (item % 2 == 0)
- {
- evenNumbers.Add(item);
- }
- }
- if (evenNumbers.Count > 0)
- {
- Console.WriteLine(evenNumbers.Take(count).Min());
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else if (commandArguments[2] == "odd")
- {
- var collectionToInt = collection.Select(int.Parse).Reverse().ToList();
- string oddCountInts = collectionToInt.Where(i => i % 2 != 0).Take(count).Select(i => i.ToString()).Aggregate((a, b) => b += String.IsNullOrEmpty(b) ? a : "," + a);
- Console.WriteLine($"[{oddCountInts}]");
- }
- }
- private static void ExecuteFirstCommand(List<string> collection, string[] commandArguments)
- {
- int count = int.Parse(commandArguments[1]);
- if (commandArguments[2] == "even")
- {
- var collectionToInt = collection.Select(int.Parse).ToList();
- string evenCountInts = collectionToInt.Where(i => i % 2 == 0).Take(count).Select(i => i.ToString()).Aggregate((a, b) => b += String.IsNullOrEmpty(b) ? a : "," + a);
- Console.WriteLine(evenCountInts);
- }
- else if (commandArguments[2] == "odd")
- {
- var collectionToInt = collection.Select(int.Parse).ToList();
- string oddCountInts = collectionToInt.Where(i => i % 2 != 0).Take(count).Select(i => i.ToString()).Aggregate((a, b) => b += String.IsNullOrEmpty(b) ? a : "," + a);
- Console.WriteLine(oddCountInts);
- }
- }
- private static void ExecuteMinCommand(List<string> collection, string[] commandArguments)
- {
- if (commandArguments[1] == "even")
- {
- var collectionToInt = collection.Select(int.Parse).ToList();
- List<int> evenNumbers = new List<int>();
- foreach (var item in collectionToInt)
- {
- if (item % 2 != 0)
- {
- evenNumbers.Remove(item);
- }
- if (item % 2 == 0)
- {
- evenNumbers.Add(item);
- }
- }
- if (evenNumbers.Count > 0)
- {
- Console.WriteLine(evenNumbers.Min());
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else if (commandArguments[1] == "odd")
- {
- var collectionToInt = collection.Select(int.Parse).ToList();
- var minOdd = collectionToInt.Where(x => x % 2 != 0).Min();
- Console.WriteLine(minOdd);
- }
- }
- private static void ExecuteMaxCommand(List<string> collection, string[] commandArguments)
- {
- if (commandArguments[1] == "even")
- {
- var collectionToInt = collection.Select(int.Parse).ToList();
- var maxEven = collectionToInt.Where(x => x % 2 == 0).Max();
- Console.WriteLine(collectionToInt.IndexOf(maxEven));
- }
- else if (commandArguments[1] == "odd")
- {
- var collectionToInt = collection.Select(int.Parse).ToList();
- var maxOdd = collectionToInt.Where(x => x % 2 != 0).Max();
- Console.WriteLine(collectionToInt.IndexOf(maxOdd));
- }
- }
- private static void ExecuteExchangeCommand(List<string> collection, string[] commandArguments)
- {
- int index = int.Parse(commandArguments[1]);
- for (int i = 0; i < index; i++)
- {
- string firstElement = collection[0];
- collection.Remove(firstElement);
- collection.Add(firstElement);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment