Advertisement
North_Point

02 – Array Manipulator

Aug 11th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.68 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 _02.Array_Manipulator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var input = Console.ReadLine().Split(' ').Select(long.Parse).ToList();
  14.            
  15.             while (true)
  16.             {
  17.                 var tokens = Console.ReadLine().Split(new char[] { ' '},StringSplitOptions.RemoveEmptyEntries);
  18.                 var command = tokens[0];
  19.                 if (command == "end")
  20.                 {
  21.                     break;
  22.                 }
  23.                 else if (command == "exchange")
  24.                 {
  25.                     var count = int.Parse(tokens[1]);
  26.                     if (count >= 0 && count < input.Count)
  27.                     {
  28.                        var booferFirst = input.Take(count + 1).ToList();
  29.                        var booferSecond = input.Skip(count + 1).ToList();
  30.                        var concat = booferSecond.Concat(booferFirst).ToList();
  31.                        input = concat;
  32.                     }
  33.                     else
  34.                     {
  35.                         Console.WriteLine("Invalid index");
  36.                     }
  37.                 }
  38.                 else if (command == "max")
  39.                 {
  40.                     var type = tokens[1];
  41.                     if (type == "odd")
  42.                     {
  43.                         if (input.Any(x => x % 2 != 0))
  44.                         {
  45.                             var maxOdd = input.Where(x => x % 2 != 0).Max();
  46.                             var re = input.LastIndexOf(maxOdd);
  47.                             Console.WriteLine(re);
  48.                         }
  49.                         else
  50.                         {
  51.                             Console.WriteLine("No matches");
  52.                         }
  53.                     }
  54.                     else if (type == "even")
  55.                     {
  56.                         if (input.Any(x => x % 2 == 0))
  57.                         {
  58.                             var maxEven = input.Where(x => x % 2 == 0).Max();
  59.                             var re = input.LastIndexOf(maxEven);
  60.                             Console.WriteLine(re);
  61.                         }
  62.                         else
  63.                         {
  64.                             Console.WriteLine("No matches");
  65.                         }
  66.                     }
  67.                 }
  68.                 else if (command == "min")
  69.                 {
  70.                     var type = tokens[1];
  71.                     if (type == "odd")
  72.                     {
  73.                         if (input.Any(x => x % 2 != 0))
  74.                         {
  75.                             var maxOdd = input.Where(x => x % 2 != 0).Min();
  76.                             var re = input.LastIndexOf(maxOdd);
  77.                             Console.WriteLine(re);
  78.                         }
  79.                         else
  80.                         {
  81.                             Console.WriteLine("No matches");
  82.                         }
  83.                     }
  84.                     else if (type == "even")
  85.                     {
  86.                         if (input.Any(x => x % 2 == 0))
  87.                         {
  88.                             var maxEven = input.Where(x => x % 2 == 0).Min();
  89.                             var re = input.LastIndexOf(maxEven);
  90.                             Console.WriteLine(re);
  91.                         }
  92.                         else
  93.                         {
  94.                             Console.WriteLine("No matches");
  95.                         }
  96.                     }
  97.                 }
  98.                 else if (command == "first")
  99.                 {
  100.                     var type = tokens[2];
  101.                     var count = int.Parse(tokens[1]);
  102.                     if (count > 0 && count < input.Count)
  103.                     {
  104.                         if (type == "odd")
  105.                         {
  106.                             var firstTake = input.Where(x => x % 2 != 0).Take(count).ToList();
  107.                             Console.WriteLine("[{0}]", string.Join(", ", firstTake));
  108.                         }
  109.                         else if (type == "even")
  110.                         {
  111.                             var firstTake = input.Where(x => x % 2 == 0).Take(count).ToList();
  112.                             Console.WriteLine("[{0}]", string.Join(", ", firstTake));
  113.                         }
  114.                     }
  115.                     else
  116.                     {
  117.                         Console.WriteLine("Invalid count");
  118.                     }
  119.                 }
  120.                 else if (command == "last")
  121.                 {
  122.                     var type = tokens[2];
  123.                     var count = int.Parse(tokens[1]);
  124.                     if (count > 0 && count < input.Count)
  125.                     {
  126.                         if (type == "odd")
  127.                         {
  128.                             var firstTake = input.Where(x => x % 2 != 0).Reverse().Take(count).Reverse().ToList();
  129.  
  130.                             Console.WriteLine("[{0}]", string.Join(", ", firstTake));
  131.                         }
  132.                         else if (type == "even")
  133.                         {
  134.                             var firstTake = input.Where(x => x % 2 == 0).Reverse().Take(count).Reverse().ToList();
  135.                             Console.WriteLine("[{0}]", string.Join(", ", firstTake));
  136.                         }
  137.                     }
  138.                     else
  139.                     {
  140.                         Console.WriteLine("Invalid count");
  141.                     }
  142.                 }
  143.             }
  144.             Console.WriteLine("[{0}]",string.Join(", ",input));
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement