Advertisement
Guest User

ArrayManipulator

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