tsekotsolov

02. Array Manipulator

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