Advertisement
Guest User

Array

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