Advertisement
Threed90

Untitled

Jun 11th, 2021
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Exr
  8. {
  9.     class Program
  10.     {
  11.  
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             int[] nums = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  16.  
  17.             while (true)
  18.             {
  19.                 string input = Console.ReadLine();
  20.  
  21.                 if(input == "end")
  22.                 {
  23.                     PrintResult(nums);
  24.                     break;
  25.                 }
  26.  
  27.                 ExecuteCommand(input, nums);
  28.             }
  29.         }
  30.  
  31.         private static void ExecuteCommand(string input, int[] nums)
  32.         {
  33.             string[] tokens = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  34.  
  35.             switch (tokens[0])
  36.             {
  37.                 case "exchange":
  38.                     int index = int.Parse(tokens[1]);
  39.                     if(index < 0 || index >= nums.Length)
  40.                     {
  41.                         Console.WriteLine("Invalid index");
  42.                     }
  43.                     else
  44.                     {
  45.                         ExchangeArray(index, nums);
  46.                     }
  47.                     break;
  48.                 case "max":
  49.                     int maxResult = GetMaxElement(tokens[1], nums);
  50.                     if(maxResult == -1)
  51.                     {
  52.                         Console.WriteLine("No matches");
  53.                     }
  54.                     else
  55.                     {
  56.                         Console.WriteLine(maxResult);
  57.                     }
  58.                     break;
  59.                 case "min":
  60.                     int minResult = GetMinElement(tokens[1], nums);
  61.                     if (minResult == -1)
  62.                     {
  63.                         Console.WriteLine("No matches");
  64.                     }
  65.                     else
  66.                     {
  67.                         Console.WriteLine(minResult);
  68.                     }
  69.                     break;
  70.                 case "first":
  71.  
  72.                     if(CheckCount(int.Parse(tokens[1]), nums))
  73.                     {
  74.                         Console.WriteLine("Invalid count");
  75.                     }
  76.                     else
  77.                     {
  78.                         int[] sequence = GetFirstElements(int.Parse(tokens[1]), tokens[2], nums);
  79.                         PrintResult(sequence);
  80.                     }
  81.                    
  82.                     break;
  83.                 case "last":
  84.                     if (CheckCount(int.Parse(tokens[1]), nums))
  85.                     {
  86.                         Console.WriteLine("Invalid count");
  87.                     }
  88.                     else
  89.                     {
  90.                         int[] sequence = GetLastElements(int.Parse(tokens[1]), tokens[2], nums);
  91.                         PrintResult(sequence);
  92.                     }
  93.                     break;
  94.             }
  95.         }
  96.  
  97.         private static void PrintResult(int[] sequnce)
  98.         {
  99.             Console.Write("[");
  100.             Console.Write(string.Join(", ", sequnce));
  101.             Console.Write("]");
  102.             Console.WriteLine();
  103.         }
  104.  
  105.         private static int[] GetLastElements(int count, string typeOfNums, int[] nums)
  106.         {
  107.             int[] elements;
  108.             if (typeOfNums == "even")
  109.             {
  110.                 elements = nums.Where(x => x % 2 == 0).ToArray();
  111.             }
  112.             else
  113.             {
  114.                 elements = nums.Where(x => x % 2 != 0).ToArray();
  115.             }
  116.  
  117.             if (elements.Length < count)
  118.             {
  119.                 count = elements.Length;
  120.             }
  121.  
  122.             return elements.TakeLast(count).ToArray();
  123.         }
  124.  
  125.         private static bool CheckCount(int count, int[] nums)
  126.         {
  127.             if(nums.Length < count)
  128.             {
  129.                 return true;
  130.             }
  131.             else
  132.             {
  133.                 return false;
  134.             }
  135.         }
  136.  
  137.         private static int[] GetFirstElements(int count, string typeOfNums, int[] nums)
  138.         {
  139.             int[] elements;
  140.             if (typeOfNums == "even")
  141.             {
  142.                 elements = nums.Where(x => x % 2 == 0).ToArray();
  143.             }
  144.             else
  145.             {
  146.                 elements = nums.Where(x => x % 2 != 0).ToArray();
  147.             }
  148.  
  149.             if(elements.Length < count)
  150.             {
  151.                 count = elements.Length;
  152.             }
  153.  
  154.             return elements.Take(count).ToArray();
  155.         }
  156.  
  157.         private static int GetMinElement(string typeOfNums, int[] nums)
  158.         {
  159.             int[] elements;
  160.             if (typeOfNums == "even")
  161.             {
  162.                 elements = nums.Where(x => x % 2 == 0).ToArray();
  163.             }
  164.             else
  165.             {
  166.                 elements = nums.Where(x => x % 2 != 0).ToArray();
  167.             }
  168.  
  169.             if (elements.Length == 0)
  170.             {
  171.                 return -1;
  172.             }
  173.             else
  174.             {
  175.                 int min = int.MaxValue;
  176.                 for (int i = elements.Length - 1; i >= 0; i--)
  177.                 {
  178.                     if (min > elements[i])
  179.                     {
  180.                         min = elements[i];
  181.                     }
  182.                 }
  183.  
  184.                 return Array.LastIndexOf(nums, min);
  185.             }
  186.         }
  187.  
  188.         private static int GetMaxElement(string typeOfNums, int[] nums)
  189.         {
  190.             int[] elements;
  191.             if(typeOfNums == "even")
  192.             {
  193.                 elements = nums.Where(x => x % 2 == 0).ToArray();
  194.             }
  195.             else
  196.             {
  197.                 elements = nums.Where(x => x % 2 != 0).ToArray();
  198.             }
  199.  
  200.             if(elements.Length == 0)
  201.             {
  202.                 return -1;
  203.             }
  204.             else
  205.             {
  206.                 int max = int.MinValue;
  207.                 for (int i = elements.Length - 1; i >= 0; i--)
  208.                 {
  209.                     if(max < elements[i])
  210.                     {
  211.                         max = elements[i];
  212.                     }
  213.                 }
  214.  
  215.                 return Array.LastIndexOf(nums, max);
  216.             }
  217.         }
  218.  
  219.         private static void ExchangeArray(int index, int[] nums)
  220.         {
  221.             int[] tempArr = new int[nums.Length];
  222.             int counter = 0;
  223.             for (int i = index +1; i <  nums.Length; i++)
  224.             {
  225.                 tempArr[counter++] = nums[i];
  226.             }
  227.  
  228.             for (int i = 0; i <= index; i++)
  229.             {
  230.                 tempArr[counter++] = nums[i];
  231.             }
  232.  
  233.             for (int i = 0; i < tempArr.Length; i++)
  234.             {
  235.                 nums[i] = tempArr[i];
  236.             }
  237.         }
  238.     }
  239.  
  240.    
  241. }
  242.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement