Advertisement
Guest User

Untitled

a guest
Jul 29th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5.  
  6. namespace ManipulatingArrays
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
  13.             string input = Console.ReadLine();
  14.  
  15.             while (input != "end")
  16.             {
  17.                 ManipulateArray(array, input);
  18.                 input = Console.ReadLine();
  19.             }
  20.  
  21.             PrintArray(array);
  22.         }
  23.  
  24.         //Determine the command and call the necessary method
  25.         public static void ManipulateArray(int[] array, string instructions)
  26.         {
  27.             //Check the first word of the instructions
  28.             string command = instructions.Split().ToArray()[0];
  29.  
  30.             if (command == "exchange")
  31.             {
  32.                 int index = int.Parse(instructions.Split().ToArray()[1]);
  33.                 array = Exchange(array, index);
  34.             }
  35.             else if (command == "max" || command == "min")
  36.             {
  37.                 string typeOfDigit = instructions.Split().ToArray()[1];
  38.                 MaxMinEvenOdd(array, command, typeOfDigit);
  39.             }
  40.             else
  41.             {
  42.                 string evenOdd = instructions.Split().ToArray()[2];
  43.                 int count = int.Parse(instructions.Split().ToArray()[1]);
  44.                 FirstLastEvenOdd(array, command, count, evenOdd);
  45.             }
  46.         }
  47.  
  48.         public static void FirstLastEvenOdd(int[] array, string firstLast, int count, string evenOdd)
  49.         {
  50.             if (count > array.Length)
  51.             {
  52.                 Console.WriteLine("Invalid count");
  53.                 return;
  54.             }
  55.  
  56.             if (evenOdd == "even")
  57.             {
  58.                 PrintFirstLastEven(array, firstLast, count);
  59.             }
  60.             else
  61.             {
  62.                 PrintFirstLastOdd(array, firstLast, count);
  63.             }
  64.  
  65.         }
  66.  
  67.         public static void PrintFirstLastEven(int[] array, string firstLast, int count)
  68.         {
  69.             if (firstLast == "first")
  70.             {
  71.                 int[] result = array.Where(x => x % 2 == 0).Take(count).ToArray();
  72.                 Console.WriteLine("[" + string.Join(", ", result) + "]");
  73.             }
  74.             else
  75.             {
  76.                 int[] result = array.Where(x => x % 2 == 0).Reverse().Take(count).Reverse().ToArray();
  77.                 Console.WriteLine("[" + string.Join(", ", result) + "]");
  78.             }
  79.         }
  80.  
  81.         public static void PrintFirstLastOdd(int[] array, string firstLast, int count)
  82.         {
  83.             if (firstLast == "first")
  84.             {
  85.                 int[] result = array.Where(x => x % 2 != 0).Take(count).ToArray();
  86.                 Console.WriteLine("[" + string.Join(", ", result) + "]");
  87.             }
  88.             else
  89.             {
  90.                 int[] result = array.Where(x => x % 2 != 0).Reverse().Take(count).Reverse().ToArray();
  91.                 Console.WriteLine("[" + string.Join(", ", result) + "]");
  92.             }
  93.         }
  94.  
  95.         public static void MaxMinEvenOdd(int[] array, string minMax, string typeOfDigit)
  96.         {
  97.             if (typeOfDigit == "even")
  98.             {
  99.                 PrintMinMaxEvenIndex(array, minMax);
  100.             }
  101.             else
  102.             {
  103.                 PrintMinMaxOddIndex(array, minMax);
  104.             }
  105.         }
  106.  
  107.         public static void PrintMinMaxEvenIndex(int[] array, string minMax)
  108.         {
  109.             int index = 0;
  110.  
  111.             int[] evens = array.Where(x => x % 2 == 0).ToArray();
  112.             if (evens.Count() == 0)
  113.             {
  114.                 Console.WriteLine("No matches");
  115.                 return;
  116.             }
  117.  
  118.             if (minMax == "min")
  119.             {
  120.                 index = Array.LastIndexOf(array, evens.Min());
  121.             }
  122.             else
  123.             {
  124.                 index = Array.LastIndexOf(array, evens.Max());
  125.             }
  126.  
  127.             Console.WriteLine(index);
  128.         }
  129.  
  130.         public static void PrintMinMaxOddIndex(int[] array, string minMax)
  131.         {
  132.             int index = 0;
  133.  
  134.             int[] odds = array.Where(x => x % 2 != 0).ToArray();
  135.             if (odds.Count() == 0)
  136.             {
  137.                 Console.WriteLine("No matches");
  138.                 return;
  139.             }
  140.  
  141.             if (minMax == "min")
  142.             {
  143.                 index = Array.LastIndexOf(array, odds.Min());
  144.             }
  145.             else
  146.             {
  147.                 index = Array.LastIndexOf(array, odds.Max());
  148.             }
  149.  
  150.             Console.WriteLine(index);
  151.         }
  152.  
  153.         public static int[] Exchange(int[] array, int index)
  154.         {
  155.             if (index >= array.Length || index < 0) { Console.WriteLine("Invalid index"); return array; }
  156.  
  157.             int[] first = array.Take(index + 1).ToArray();
  158.             int[] second = array.Skip(index + 1).ToArray();
  159.  
  160.             List<int> exchanged = new List<int>(second);
  161.             exchanged.AddRange(first);
  162.  
  163.             return exchanged.ToArray();
  164.         }
  165.  
  166.         public static void PrintArray(int[] array)
  167.         {
  168.             Console.WriteLine("[" + string.Join(", ", array) + "]");
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement