Advertisement
a1m

Untitled

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