deleriumbg

Array Manipulator

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