-Annie-

ArrayManipulator

Jun 15th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. namespace ArrayManipulator
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class ArrayManipulator
  8.     {
  9.         public static void Main()
  10.         {
  11.             List<string> collection = Console.ReadLine()
  12.                 .Split(new[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries)
  13.                 .ToList();
  14.  
  15.             string command = Console.ReadLine();
  16.  
  17.             while (command != "end")
  18.             {
  19.                 string[] commandArguments = command.Split(new[] { ' ', ',' });
  20.  
  21.                 switch (commandArguments[0])
  22.                 {
  23.                     case "exchange":
  24.                         ExecuteExchangeCommand(collection, commandArguments);
  25.                         Console.WriteLine("[{0}]", string.Join(", ", collection));
  26.                         break;
  27.                     case "max":
  28.                         ExecuteMaxCommand(collection, commandArguments);
  29.                         break;
  30.                     case "min":
  31.                         ExecuteMinCommand(collection, commandArguments);
  32.                         break;
  33.                     case "first":
  34.                         ExecuteFirstCommand(collection, commandArguments);
  35.                         break;
  36.                     case "last":
  37.                         ExecuteLastCommand(collection, commandArguments);
  38.                         break;
  39.                 }
  40.  
  41.                 command = Console.ReadLine();
  42.             }
  43.         }
  44.  
  45.         private static void ExecuteLastCommand(List<string> collection, string[] commandArguments)
  46.         {
  47.             int count = int.Parse(commandArguments[1]);
  48.  
  49.             if (commandArguments[2] == "even")
  50.             {
  51.                 var collectionToInt = collection.Select(int.Parse).ToList();
  52.                 List<int> evenNumbers = new List<int>();
  53.                 foreach (var item in collectionToInt)
  54.                 {
  55.                     if (item % 2 != 0)
  56.                     {
  57.                         evenNumbers.Remove(item);
  58.                     }
  59.  
  60.                     if (item % 2 == 0)
  61.                     {
  62.                         evenNumbers.Add(item);
  63.                     }
  64.                 }
  65.  
  66.                 if (evenNumbers.Count > 0)
  67.                 {
  68.                     Console.WriteLine(evenNumbers.Take(count).Min());
  69.                 }
  70.  
  71.                 else
  72.                 {
  73.                     Console.WriteLine("No matches");
  74.                 }
  75.             }
  76.  
  77.             else if (commandArguments[2] == "odd")
  78.             {
  79.                 var collectionToInt = collection.Select(int.Parse).Reverse().ToList();
  80.                 string oddCountInts = collectionToInt.Where(i => i % 2 != 0).Take(count).Select(i => i.ToString()).Aggregate((a, b) => b += String.IsNullOrEmpty(b) ? a : "," + a);
  81.                 Console.WriteLine($"[{oddCountInts}]");
  82.             }
  83.         }
  84.  
  85.         private static void ExecuteFirstCommand(List<string> collection, string[] commandArguments)
  86.         {
  87.             int count = int.Parse(commandArguments[1]);
  88.  
  89.             if (commandArguments[2] == "even")
  90.             {
  91.                 var collectionToInt = collection.Select(int.Parse).ToList();
  92.                 string evenCountInts = collectionToInt.Where(i => i % 2 == 0).Take(count).Select(i => i.ToString()).Aggregate((a, b) => b += String.IsNullOrEmpty(b) ? a : "," + a);
  93.                 Console.WriteLine(evenCountInts);
  94.             }
  95.  
  96.             else if (commandArguments[2] == "odd")
  97.             {
  98.                 var collectionToInt = collection.Select(int.Parse).ToList();
  99.                 string oddCountInts = collectionToInt.Where(i => i % 2 != 0).Take(count).Select(i => i.ToString()).Aggregate((a, b) => b += String.IsNullOrEmpty(b) ? a : "," + a);
  100.                 Console.WriteLine(oddCountInts);
  101.             }
  102.         }
  103.  
  104.         private static void ExecuteMinCommand(List<string> collection, string[] commandArguments)
  105.         {
  106.             if (commandArguments[1] == "even")
  107.             {
  108.                 var collectionToInt = collection.Select(int.Parse).ToList();
  109.                 List<int> evenNumbers = new List<int>();
  110.                 foreach (var item in collectionToInt)
  111.                 {
  112.                     if (item % 2 != 0)
  113.                     {
  114.                         evenNumbers.Remove(item);
  115.                     }
  116.  
  117.                     if (item % 2 == 0)
  118.                     {
  119.                         evenNumbers.Add(item);
  120.                     }
  121.                 }
  122.  
  123.                 if (evenNumbers.Count > 0)
  124.                 {
  125.                     Console.WriteLine(evenNumbers.Min());
  126.                 }
  127.  
  128.                 else
  129.                 {
  130.                     Console.WriteLine("No matches");
  131.                 }        
  132.             }
  133.  
  134.             else if (commandArguments[1] == "odd")
  135.             {
  136.                 var collectionToInt = collection.Select(int.Parse).ToList();
  137.                 var minOdd = collectionToInt.Where(x => x % 2 != 0).Min();
  138.  
  139.                 Console.WriteLine(minOdd);
  140.             }
  141.         }
  142.  
  143.         private static void ExecuteMaxCommand(List<string> collection, string[] commandArguments)
  144.         {
  145.             if (commandArguments[1] == "even")
  146.             {
  147.                 var collectionToInt = collection.Select(int.Parse).ToList();
  148.                 var maxEven = collectionToInt.Where(x => x % 2 == 0).Max();
  149.  
  150.                 Console.WriteLine(collectionToInt.IndexOf(maxEven));
  151.             }
  152.  
  153.             else if (commandArguments[1] == "odd")
  154.             {
  155.                 var collectionToInt = collection.Select(int.Parse).ToList();
  156.                 var maxOdd = collectionToInt.Where(x => x % 2 != 0).Max();
  157.  
  158.                 Console.WriteLine(collectionToInt.IndexOf(maxOdd));
  159.             }
  160.         }
  161.  
  162.         private static void ExecuteExchangeCommand(List<string> collection, string[] commandArguments)
  163.         {
  164.             int index = int.Parse(commandArguments[1]);
  165.  
  166.             for (int i = 0; i < index; i++)
  167.             {
  168.                 string firstElement = collection[0];
  169.                 collection.Remove(firstElement);
  170.                 collection.Add(firstElement);
  171.             }
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment