Advertisement
YavorGrancharov

02.Array_Manipulator

Nov 4th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.Array_Manipulator
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<int> list = Console.ReadLine().Split().Select(int.Parse).ToList();
  12.  
  13.             string input = Console.ReadLine();
  14.             while (input != "end")
  15.             {
  16.                 string[] commands = input.Split();
  17.                 if (commands.Length == 2)
  18.                 {
  19.                     string command = commands[0];
  20.                     switch (command)
  21.                     {
  22.                         case "exchange":
  23.                             int index = int.Parse(commands[1]);
  24.                             list = Exchange(list, index);
  25.                             break;
  26.                         case "max":
  27.                             Max(list, commands[1]);
  28.                             break;
  29.                         case "min":
  30.                             Min(list, commands[1]);
  31.                             break;
  32.                     }
  33.                 }
  34.                 else
  35.                 {
  36.                     string order = commands[0];
  37.                     int count = int.Parse(commands[1]);
  38.                     string comm = commands[2];
  39.                     FirstLast(list, order, count, comm);
  40.                 }
  41.                 input = Console.ReadLine();
  42.             }
  43.             Console.WriteLine("[{0}]", string.Join(", ", list));
  44.         }
  45.         private static List<int> Exchange(List<int> list, int index)
  46.         {
  47.             List<int> ex = new List<int>();
  48.             if (index < 0 || index > list.Count - 1)
  49.             {
  50.                 Console.WriteLine("Invalid index");
  51.             }
  52.             else
  53.             {              
  54.                 ex = list.Skip(index + 1).ToList();
  55.                 ex.AddRange(list.Take(index + 1).ToList());
  56.                 list = ex;
  57.             }
  58.             return list;
  59.         }
  60.         private static void Max(List<int> list, string comm)
  61.         {
  62.             List<int> data = new List<int>();
  63.             if (comm == "even")
  64.             {
  65.                 data = list.Where(x => (x % 2) == 0).ToList();
  66.             }
  67.             else if (comm == "odd")
  68.             {
  69.                 data = list.Where(x => (x % 2) != 0).ToList();
  70.             }
  71.             if (data.Count != 0)
  72.             {
  73.                 int Max = data.Max();
  74.                 Console.WriteLine(list.LastIndexOf(Max));
  75.             }
  76.             else
  77.             {
  78.                 Console.WriteLine("No matches");
  79.             }
  80.         }
  81.         private static void Min(List<int> list, string comm)
  82.         {
  83.             List<int> data = new List<int>();
  84.             if (comm == "even")
  85.             {
  86.                 data = list.Where(x => (x % 2) == 0).ToList();
  87.             }
  88.             else if (comm == "odd")
  89.             {
  90.                 data = list.Where(x => (x % 2) != 0).ToList();
  91.             }
  92.             if (data.Count != 0)
  93.             {
  94.                 int Min = data.Min();
  95.                 Console.WriteLine(list.LastIndexOf(Min));
  96.             }
  97.             else
  98.             {
  99.                 Console.WriteLine("No matches");
  100.             }
  101.         }
  102.         private static void FirstLast(List<int> list, string order, int count, string comm)
  103.         {
  104.             List<int> odds = list.Where(x => (x % 2) != 0).ToList();
  105.             List<int> evens = list.Where(x => (x % 2) == 0).ToList();
  106.             if (count > list.Count)
  107.             {
  108.                 Console.WriteLine("Invalid count");
  109.             }
  110.             else
  111.             {
  112.                 if (order == "first")
  113.                 {
  114.                     if (comm == "odd")
  115.                     {
  116.                         if (odds.Count == 0)
  117.                         {
  118.                             Console.WriteLine("[]");
  119.                         }
  120.                         else if (count > odds.Count)
  121.                         {
  122.                             Console.WriteLine("[{0}]",string.Join(", ",odds));
  123.                         }
  124.                         else
  125.                         {
  126.                             Console.WriteLine("[{0}]",string.Join(", ",odds.Take(count).ToList()));
  127.                         }
  128.                     }
  129.                     else if (comm == "even")
  130.                     {
  131.                         if (evens.Count == 0)
  132.                         {
  133.                             Console.WriteLine("[]");
  134.                         }
  135.                         else if (count > evens.Count)
  136.                         {
  137.                             Console.WriteLine("[{0}]", string.Join(", ", evens));
  138.                         }
  139.                         else
  140.                         {
  141.                             Console.WriteLine("[{0}]", string.Join(", ", evens.Take(count).ToList()));
  142.                         }
  143.                     }
  144.                 }
  145.                 else if (order == "last")
  146.                 {
  147.                     if (comm == "odd")
  148.                     {
  149.                         if (odds.Count == 0)
  150.                         {
  151.                             Console.WriteLine("[]");
  152.                         }
  153.                         else if (count > odds.Count)
  154.                         {
  155.                             Console.WriteLine("[{0}]", string.Join(", ", odds));
  156.                         }
  157.                         else
  158.                         {
  159.                             List<int> temp = odds.Skip(odds.Count - count).Take(count).ToList();
  160.                             Console.WriteLine("[{0}]", string.Join(", ", temp));
  161.                         }
  162.                     }
  163.                     else if (comm == "even")
  164.                     {
  165.                         if (evens.Count == 0)
  166.                         {
  167.                             Console.WriteLine("[]");
  168.                         }
  169.                         else if (count > evens.Count)
  170.                         {
  171.                             Console.WriteLine("[{0}]", string.Join(", ", evens));
  172.                         }
  173.                         else
  174.                         {
  175.                             List<int> temp = evens.Skip(evens.Count - count).Take(count).ToList();
  176.                             Console.WriteLine("[{0}]", string.Join(", ", temp));
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement