LardaX

Array-Manipulator

Oct 22nd, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.58 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 Array_Manipulator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
  14.  
  15.             string command = Console.ReadLine().ToLower();
  16.  
  17.             while (!command.Equals("end"))
  18.             {
  19.                 string[] commandArgs = command.Split().ToArray();
  20.                 switch (commandArgs[0])
  21.                 {
  22.                     case "exchange":
  23.                         Exchange(commandArgs, numbers);
  24.                         break;
  25.  
  26.                     case "max":
  27.                         PrintMax(commandArgs, numbers);
  28.                         break;
  29.                     case "min":
  30.                         PrintMin(commandArgs, numbers);
  31.                         break;
  32.                     case "first":
  33.                         PrintFirstElements(commandArgs, numbers);
  34.                         break;
  35.                     case "last":
  36.                         PrintLastElements(commandArgs, numbers);
  37.                         break;
  38.                 }
  39.  
  40.                 command = Console.ReadLine().ToLower();
  41.             }
  42.  
  43.             Console.WriteLine("[" + string.Join(", ", numbers) + "]");
  44.         }
  45.  
  46.         private static void PrintLastElements(string[] commandArgs, List<int> numbers)
  47.         {
  48.             int count = int.Parse(commandArgs[1]);
  49.  
  50.             if (count > numbers.Count || count < 0)
  51.             {
  52.                 Console.WriteLine("Invalid count");
  53.             }
  54.             else
  55.             {
  56.                 string type = commandArgs[2];
  57.                 int remainder = type == "even" ? 0 : 1;
  58.  
  59.                 int[] elements = numbers.Where(x => Math.Abs(x) % 2 == remainder).Reverse().Take(count).Reverse().ToArray();
  60.                 if (elements.Length.Equals(0))
  61.                 {
  62.                     Console.WriteLine("[]");
  63.                 }
  64.                 else
  65.                 {
  66.                     Console.WriteLine("[" + string.Join(", ", elements) + "]");
  67.                 }
  68.             }
  69.         }
  70.         private static void PrintFirstElements(string[] commandArgs, List<int> numbers)
  71.         {
  72.             int count = int.Parse(commandArgs[1]);
  73.  
  74.             if (count > numbers.Count || count < 0)
  75.             {
  76.                 Console.WriteLine("Invalid count");
  77.             }
  78.             else
  79.             {
  80.                 string type = commandArgs[2];
  81.                 int remainder = type == "even" ? 0 : 1;
  82.                 int[] elements = numbers.Where(x => Math.Abs(x) % 2 == remainder).Take(count).ToArray();
  83.                 if (elements.Length.Equals(0))
  84.                 {
  85.                     Console.WriteLine("[]");
  86.  
  87.                 }
  88.                 else
  89.                 {
  90.                     Console.WriteLine("[" + string.Join(", ", elements) + "]");
  91.                 }
  92.             }
  93.         }
  94.  
  95.         private static void PrintMin(string[] commandArgs, List<int> numbers)
  96.         {
  97.             string evenOrOdd = commandArgs[1];
  98.             if (evenOrOdd.Equals("even"))
  99.             {
  100.                 try
  101.                 {
  102.                     int min = numbers.Where(x => Math.Abs(x) % 2 == 0).Min();
  103.                     int minIndex = numbers.LastIndexOf(min);
  104.                     Console.WriteLine(minIndex);
  105.                 }
  106.                 catch (Exception)
  107.                 {
  108.                     Console.WriteLine("No matches");
  109.                 }
  110.             }
  111.             else if (evenOrOdd.Equals("odd"))
  112.             {
  113.                 try
  114.                 {
  115.                     int min = numbers.Where(x => Math.Abs(x) % 2 == 1).Min();
  116.                     int minIndex = numbers.LastIndexOf(min);
  117.                     Console.WriteLine(minIndex);
  118.                 }
  119.                 catch (Exception)
  120.                 {
  121.                     Console.WriteLine("No matches");
  122.                 }
  123.             }
  124.         }
  125.  
  126.         private static void PrintMax(string[] commandArgs, List<int> numbers)
  127.         {
  128.             string evenOrOdd = commandArgs[1];
  129.             if (evenOrOdd.Equals("even"))
  130.             {
  131.                 try
  132.                 {
  133.                     int max = numbers.Where(x => Math.Abs(x) % 2 == 0).Max();
  134.                     int maxIndex = numbers.LastIndexOf(max);
  135.                     Console.WriteLine(maxIndex);
  136.                 }
  137.                 catch (Exception)
  138.                 {
  139.                     Console.WriteLine("No matches");
  140.                 }
  141.  
  142.             }
  143.             else if (evenOrOdd.Equals("odd"))
  144.             {
  145.                 try
  146.                 {
  147.                     int max = numbers.Where(x => Math.Abs(x) % 2 == 1).Max();
  148.                     int maxIndex = numbers.LastIndexOf(max);
  149.                     Console.WriteLine(maxIndex);
  150.                 }
  151.                 catch (Exception)
  152.                 {
  153.                     Console.WriteLine("No matches");
  154.                 }
  155.             }
  156.         }
  157.  
  158.         private static void Exchange(string[] commandArgs, List<int> numbers)
  159.         {
  160.             int index = int.Parse(commandArgs[1]);
  161.             if (index < 0 || index >= numbers.Count)
  162.             {
  163.                 Console.WriteLine("Invalid index");
  164.             }
  165.             else
  166.             {
  167.                 List<int> firstGroup = numbers.Take(index+1).ToList();
  168.                 numbers.RemoveRange(0, index + 1);
  169.                 numbers.AddRange(firstGroup);
  170.             }
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment