simonradev

ArrayManipulator

Dec 8th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ArrayManipulator
  8. {
  9.     class ArrayManipulator
  10.     {
  11.         static List<int> array;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             array = Console.ReadLine().Split().Select(int.Parse).ToList();
  16.  
  17.             string inputLine = Console.ReadLine().ToLower();
  18.             while (!inputLine.Equals("end"))
  19.             {
  20.                 string[] commandInfo = inputLine.Split().ToArray();
  21.  
  22.                 if (commandInfo[0].Equals("exchange"))
  23.                 {
  24.                     int index = int.Parse(commandInfo[1]);
  25.  
  26.                     if (index < 0 || index >= array.Count)
  27.                     {
  28.                         Console.WriteLine("Invalid index");
  29.                         inputLine = Console.ReadLine().ToLower();
  30.                         continue;
  31.                     }
  32.  
  33.                     int[] subSequence = array.TakeWhile((x, i) => i <= index).ToArray();
  34.                     array.RemoveRange(0, index + 1);
  35.                     array.InsertRange(array.Count, subSequence);
  36.                 }
  37.                 else if (commandInfo[0].Equals("max") && commandInfo[1].Equals("even"))
  38.                 {
  39.                     int maxEven = 0;
  40.                     int index = 0;
  41.                     try
  42.                     {
  43.                         maxEven = array.Where(x => x % 2 == 0).Max();
  44.                         index = array.LastIndexOf(maxEven);
  45.                     }
  46.                     catch (Exception)
  47.                     {
  48.                         Console.WriteLine("No matches");
  49.                         inputLine = Console.ReadLine().ToLower();
  50.                         continue;
  51.                     }
  52.  
  53.                     Console.WriteLine(index);
  54.                 }
  55.                 else if (commandInfo[0].Equals("max") && commandInfo[1].Equals("odd"))
  56.                 {
  57.                     int maxOdd = 0;
  58.                     int index = 0;
  59.                     try
  60.                     {
  61.                         maxOdd = array.Where(x => x % 2 != 0).Max();
  62.                         index = array.LastIndexOf(maxOdd);
  63.                     }
  64.                     catch (Exception)
  65.                     {
  66.                         Console.WriteLine("No matches");
  67.                         inputLine = Console.ReadLine().ToLower();
  68.                         continue;
  69.                     }
  70.  
  71.                     Console.WriteLine(index);
  72.                 }
  73.                 else if (commandInfo[0].Equals("min") && commandInfo[1].Equals("even"))
  74.                 {
  75.                     int minEven = 0;
  76.                     int index = 0;
  77.                     try
  78.                     {
  79.                         minEven = array.Where(x => x % 2 == 0).Min();
  80.                         index = array.LastIndexOf(minEven);
  81.                     }
  82.                     catch (Exception)
  83.                     {
  84.                         Console.WriteLine("No matches");
  85.                         inputLine = Console.ReadLine().ToLower();
  86.                         continue;
  87.                     }
  88.  
  89.                     Console.WriteLine(index);
  90.                 }
  91.                 else if (commandInfo[0].Equals("min") && commandInfo[1].Equals("odd"))
  92.                 {
  93.                     int minOdd = 0;
  94.                     int index = 0;
  95.                     try
  96.                     {
  97.                         minOdd = array.Where(x => x % 2 != 0).Min();
  98.                         index = array.LastIndexOf(minOdd);
  99.                     }
  100.                     catch (Exception)
  101.                     {
  102.                         Console.WriteLine("No matches");
  103.                         inputLine = Console.ReadLine().ToLower();
  104.                         continue;
  105.                     }
  106.  
  107.                     Console.WriteLine(index);
  108.                 }
  109.                 else if (commandInfo[0].Equals("first") && commandInfo[2].Equals("even"))
  110.                 {
  111.                     int count = int.Parse(commandInfo[1]);
  112.  
  113.                     if (count > array.Count)
  114.                     {
  115.                         Console.WriteLine("Invalid count");
  116.                         inputLine = Console.ReadLine().ToLower();
  117.                         continue;
  118.                     }
  119.  
  120.                     List<int> firstEven = new List<int>();
  121.                     foreach (int num in array)
  122.                     {
  123.                         if (firstEven.Count == count)
  124.                         {
  125.                             break;
  126.                         }
  127.  
  128.                         if (num % 2 == 0)
  129.                         {
  130.                             firstEven.Add(num);
  131.                         }
  132.                     }
  133.  
  134.                     Console.WriteLine($"[{string.Join(", ", firstEven)}]");
  135.                 }
  136.                 else if (commandInfo[0].Equals("first") && commandInfo[2].Equals("odd"))
  137.                 {
  138.                     int count = int.Parse(commandInfo[1]);
  139.  
  140.                     if (count > array.Count)
  141.                     {
  142.                         Console.WriteLine("Invalid count");
  143.                         inputLine = Console.ReadLine().ToLower();
  144.                         continue;
  145.                     }
  146.  
  147.                     List<int> firstOdd = new List<int>();
  148.                     foreach (int num in array)
  149.                     {
  150.                         if (firstOdd.Count == count)
  151.                         {
  152.                             break;
  153.                         }
  154.  
  155.                         if (num % 2 != 0)
  156.                         {
  157.                             firstOdd.Add(num);
  158.                         }
  159.                     }
  160.  
  161.                     Console.WriteLine($"[{string.Join(", ", firstOdd)}]");
  162.                 }
  163.                 else if (commandInfo[0].Equals("last") && commandInfo[2].Equals("even"))
  164.                 {
  165.                     int count = int.Parse(commandInfo[1]);
  166.  
  167.                     if (count > array.Count)
  168.                     {
  169.                         Console.WriteLine("Invalid count");
  170.                         inputLine = Console.ReadLine().ToLower();
  171.                         continue;
  172.                     }
  173.  
  174.                     List<int> lastEven = new List<int>();
  175.                     for (int currElement = array.Count - 1; currElement >= 0; currElement--)
  176.                     {
  177.                         if (lastEven.Count == count)
  178.                         {
  179.                             break;
  180.                         }
  181.  
  182.                         if (array[currElement] % 2 == 0)
  183.                         {
  184.                             lastEven.Add(array[currElement]);
  185.                         }
  186.                     }
  187.  
  188.                     Console.WriteLine($"[{string.Join(", ", lastEven)}]");
  189.                 }
  190.                 else if (commandInfo[0].Equals("last") && commandInfo[2].Equals("odd"))
  191.                 {
  192.                     int count = int.Parse(commandInfo[1]);
  193.  
  194.                     if (count > array.Count)
  195.                     {
  196.                         Console.WriteLine("Invalid count");
  197.                         inputLine = Console.ReadLine().ToLower();
  198.                         continue;
  199.                     }
  200.  
  201.                     List<int> lastOdd = new List<int>();
  202.                     for (int currElement = array.Count - 1; currElement >= 0; currElement--)
  203.                     {
  204.                         if (lastOdd.Count == count)
  205.                         {
  206.                             break;
  207.                         }
  208.  
  209.                         if (array[currElement] % 2 != 0)
  210.                         {
  211.                             lastOdd.Add(array[currElement]);
  212.                         }
  213.                     }
  214.  
  215.                     Console.WriteLine($"[{string.Join(", ", lastOdd)}]");
  216.                 }
  217.  
  218.                 inputLine = Console.ReadLine().ToLower();
  219.             }
  220.  
  221.             Console.WriteLine($"[{string.Join(", ", array)}]");
  222.         }
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment