Advertisement
Guest User

Array Manipulator

a guest
Jun 28th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Numerics;
  6.  
  7. namespace _02_SecondExercise
  8. {
  9.     class KaminoFactory
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> arr = Console.ReadLine().Split().Select(int.Parse).ToList();
  14.             string input = Console.ReadLine();
  15.  
  16.             while (input!="end")
  17.             {
  18.                 string[] tokens = input.Split().ToArray();
  19.                 string command = tokens[0];
  20.                 if (command== "exchange")
  21.                 {
  22.                     int index = int.Parse(tokens[1]);
  23.                     if (index<arr.Count)
  24.                     {
  25.                       index++;
  26.                         var temp = arr.Take(index);
  27.                         arr = arr.Skip(index).ToList();
  28.                         arr = arr.Concat(temp).ToList();
  29.                     }
  30.                     else
  31.                     {
  32.                         Console.WriteLine("Invalid index");
  33.                     }
  34.                 }
  35.                 if (command=="max")
  36.                 {
  37.                     string evenOdd = tokens[1];
  38.                     if (evenOdd == "even")
  39.                     {
  40.                         int max = int.MinValue;
  41.                         int indexEven =-1;
  42.                         for (int i = 0; i < arr.Count; i++)
  43.                         {
  44.                             if (arr[i] % 2 == 0)
  45.                             {
  46.                                 if (arr[i] >= max)
  47.                                 {
  48.                                     max = arr[i];
  49.                                     indexEven = i;
  50.                                 }
  51.                             }
  52.                         }
  53.                         if (indexEven == -1)
  54.                         {
  55.                             Console.WriteLine("No matches");
  56.                         }
  57.                         else { Console.WriteLine(indexEven); }
  58.                        
  59.                     }
  60.                     else if (evenOdd == "odd")
  61.                     {
  62.                         int max = int.MinValue;
  63.                         int indexOdd = -1;
  64.                         for (int i = 0; i < arr.Count; i++)
  65.                         {
  66.                             if (arr[i] % 2 != 0)
  67.                             {
  68.                                 if (arr[i] >= max)
  69.                                 {
  70.                                     max = arr[i];
  71.                                     indexOdd = i;
  72.                                 }
  73.                             }
  74.                         }
  75.                         if (indexOdd == -1)
  76.                         {
  77.                             Console.WriteLine("No matches");
  78.                         }
  79.                         else { Console.WriteLine(indexOdd); }
  80.                     }
  81.                 }
  82.                 if (command == "min")
  83.                 {
  84.                     string evenOdd = tokens[1];
  85.                     if (evenOdd == "even")
  86.                     {
  87.                         int min = int.MaxValue;
  88.                         int indexEven = -1;
  89.                         for (int i = 0; i < arr.Count; i++)
  90.                         {
  91.                             if (arr[i] % 2 == 0)
  92.                             {
  93.                                 if (arr[i] <= min)
  94.                                 {
  95.                                     min = arr[i];
  96.                                     indexEven = i;
  97.                                 }
  98.                             }
  99.                         }
  100.                         if (indexEven == -1)
  101.                         {
  102.                             Console.WriteLine("No matches");
  103.                         }
  104.                         else { Console.WriteLine(indexEven); }
  105.  
  106.                     }
  107.                     else if (evenOdd == "odd")
  108.                     {
  109.                         int min = int.MaxValue;
  110.                         int indexOdd = -1;
  111.                         for (int i = 0; i < arr.Count; i++)
  112.                         {
  113.                             if (arr[i] % 2 != 0)
  114.                             {
  115.                                 if (arr[i] <= min)
  116.                                 {
  117.                                     min = arr[i];
  118.                                     indexOdd = i;
  119.                                 }
  120.                             }
  121.                         }
  122.                         if (indexOdd == -1)
  123.                         {
  124.                             Console.WriteLine("No matches");
  125.                         }
  126.                         else { Console.WriteLine(indexOdd); }
  127.                     }
  128.                 }
  129.                 if (command =="first")
  130.                 {
  131.                     int count = int.Parse(tokens[1]);
  132.                     if (count > arr.Count)
  133.                     {
  134.                         Console.WriteLine("Invalid count");
  135.                     }
  136.                     else
  137.                     {
  138.                         string seccommand = tokens[2];
  139.                         List<int> firstEven = new List<int>();
  140.                         List<int> firstOdd = new List<int>();
  141.                         if (seccommand == "even")
  142.                         {
  143.                             for (int i = 0; i < arr.Count; i++)
  144.                             {
  145.                                 if (arr[i] % 2 == 0)
  146.                                 {
  147.                                     firstEven.Add(arr[i]);
  148.                                 }
  149.                             }
  150.                             firstEven = firstEven.Take(count).ToList();
  151.                             Console.WriteLine($"[" + string.Join(", ", firstEven) + "]");
  152.                         }
  153.                         else if (seccommand == "odd")
  154.                         {
  155.                             for (int i = 0; i < arr.Count; i++)
  156.                             {
  157.                                 if (arr[i] % 2 != 0)
  158.                                 {
  159.                                     firstOdd.Add(arr[i]);
  160.                                 }
  161.                             }
  162.                             firstOdd = firstOdd.Take(count).ToList();
  163.                             Console.WriteLine($"[" + string.Join(", ", firstOdd) + "]");
  164.                         }
  165.                     }
  166.  
  167.                 }
  168.                 if (command =="last")
  169.                 {
  170.                     int count = int.Parse(tokens[1]);
  171.                     if (count > arr.Count)
  172.                     {
  173.                         Console.WriteLine("Invalid count");
  174.                     }
  175.                     else
  176.                     {
  177.                         string seccommand = tokens[2];
  178.                         List<int> lastEven = new List<int>();
  179.                         List<int> lastOdd = new List<int>();
  180.                         if (seccommand == "even")
  181.                         {
  182.                             for (int i = 0; i < arr.Count; i++)
  183.                             {
  184.                                 if (arr[i] % 2 == 0)
  185.                                 {
  186.                                     lastEven.Add(arr[i]);
  187.                                 }
  188.                             }
  189.                             lastEven = lastEven.Take(count).ToList();
  190.                             Console.WriteLine($"[" + string.Join(", ", lastEven) + "]");
  191.                         }
  192.                         else if (seccommand == "odd")
  193.                         {
  194.                             for (int i = 0; i < arr.Count; i++)
  195.                             {
  196.                                 if (arr[i] % 2 != 0)
  197.                                 {
  198.                                     lastOdd.Add(arr[i]);
  199.                                 }
  200.                             }
  201.                             lastEven = lastEven.Take(count).ToList();
  202.                             Console.WriteLine($"[" + string.Join(", ", lastOdd) + "]");
  203.                         }
  204.                     }
  205.  
  206.                 }
  207.                 input = Console.ReadLine();
  208.             }
  209.             Console.WriteLine("["+string.Join(", ",arr)+"]");
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement