Advertisement
Guest User

Untitled

a guest
Jul 30th, 2019
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _11._Array_Manipulator
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int[] arr = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  11.  
  12.             string input = Console.ReadLine();
  13.  
  14.             while (input != "end")
  15.             {
  16.                 string[] commands = input.Split(" ");
  17.  
  18.                 if (commands[0] == "exchange")
  19.                 {
  20.                     int splitIndex = int.Parse(commands[1]);
  21.                     if (splitIndex < arr.Length && splitIndex >= 0)
  22.                     {
  23.                         Exchange(arr, splitIndex);
  24.                     }
  25.                     else
  26.                     {
  27.                         Console.WriteLine("Invalid index");
  28.                     }
  29.                 }
  30.  
  31.                 else if (commands[0] == "max" || commands[0] == "min")
  32.                 {
  33.                     Console.WriteLine(EvenOddMaxMin(arr, commands[0], commands[1]));
  34.                 }
  35.  
  36.                 else if (commands[0] == "first" || commands[0] == "last")
  37.                 {
  38.                     int count = int.Parse(commands[1]);
  39.  
  40.                     if (count <= arr.Length)
  41.                     {
  42.                         Console.WriteLine("[" + FirstLast(EvenOdd(arr, commands[2]), commands[0], count) + "]");
  43.                     }
  44.                     else
  45.                     {
  46.                         Console.WriteLine("Invalid count");
  47.                     }
  48.                 }
  49.  
  50.                 input = Console.ReadLine();
  51.             }
  52.  
  53.             Console.WriteLine("[" + string.Join(", ", arr) + "]");
  54.         }
  55.  
  56.         static void Exchange(int[] arr, int splitIndex)
  57.         {
  58.             int[] exchangedArr = new int[arr.Length];
  59.             int indexExchArr = 0;
  60.  
  61.             for (int i = splitIndex + 1; i < arr.Length; i++)
  62.             {
  63.                 exchangedArr[indexExchArr] = arr[i];
  64.                 indexExchArr++;
  65.             }
  66.  
  67.             for (int i = 0; i <= splitIndex; i++)
  68.             {
  69.                 exchangedArr[indexExchArr] = arr[i];
  70.                 indexExchArr++;
  71.             }
  72.  
  73.             Array.Copy(exchangedArr, arr, arr.Length);
  74.         }
  75.  
  76.         static string EvenOddMaxMin(int[] arr, string maxMin, string evenOdd)
  77.         {
  78.             int index = -1;
  79.             int max = int.MinValue;
  80.             int min = int.MaxValue;
  81.  
  82.             int resultFromModDiv = 0;
  83.             if (evenOdd == "odd")
  84.             {
  85.                 resultFromModDiv = 1;
  86.             }
  87.  
  88.             for (int i = 0; i < arr.Length; i++)
  89.             {
  90.                 if (arr[i] % 2 == resultFromModDiv)
  91.                 {
  92.                     if (maxMin == "min")
  93.                     {
  94.                         if (arr[i] <= min)
  95.                         {
  96.                             index = i;
  97.                             min = arr[i];
  98.                         }
  99.                     }
  100.                     else if (maxMin == "max")
  101.                     {
  102.                         if (arr[i] >= max)
  103.                         {
  104.                             index = i;
  105.                             max = arr[i];
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.  
  111.             if (index >= 0)
  112.             {
  113.                 return index.ToString();
  114.             }
  115.  
  116.             return "No matches";
  117.         }
  118.  
  119.         static int[] EvenOdd(int[] arr, string evenOdd)
  120.         {
  121.             int[] evenOrOdd = new int[arr.Length];
  122.             int index = 0;
  123.  
  124.             int resultFromModDiv = 0;
  125.             if (evenOdd == "odd")
  126.             {
  127.                 resultFromModDiv = 1;
  128.             }
  129.  
  130.             for (int i = 0; i < arr.Length; i++)
  131.             {
  132.                 if (arr[i] % 2 == resultFromModDiv)
  133.                 {
  134.                     evenOrOdd[index] = arr[i];
  135.                     index++;
  136.                 }
  137.             }
  138.  
  139.             arr = new int[index];
  140.             Array.Copy(evenOrOdd, arr, index);
  141.  
  142.             return arr;
  143.         }
  144.  
  145.         static string FirstLast(int[] arr, string firstLast, int count)
  146.         {
  147.             int[] newArr = new int[arr.Length];
  148.             int index = 0;
  149.  
  150.             if (firstLast == "first")
  151.             {
  152.                 for (int i = 0; i < count && i < arr.Length; i++)
  153.                 {
  154.                     newArr[index] = arr[i];
  155.                     index++;
  156.                 }
  157.             }
  158.             else if (firstLast == "last")
  159.             {
  160.                 if (count > arr.Length)
  161.                 {
  162.                     count = arr.Length;
  163.                 }
  164.  
  165.                 for (int i = arr.Length - count; i < arr.Length; i++)
  166.                 {
  167.                     newArr[index] = arr[i];
  168.                     index++;
  169.                 }
  170.             }
  171.  
  172.             arr = new int[index];
  173.  
  174.             Array.Copy(newArr, arr, index);
  175.  
  176.             return string.Join(", ", arr);
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement