Advertisement
silvana1303

array manipulator

Jun 22nd, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Numerics;
  5.  
  6. namespace exampreparation
  7. {
  8.     class exam
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
  13.  
  14.             string[] command = Console.ReadLine().Split().ToArray();
  15.  
  16.             int indexResult = 0;
  17.  
  18.             while (command[0] != "end")
  19.             {
  20.                 if (command[0] == "exchange")
  21.                 {
  22.                     int index = int.Parse(command[1]);
  23.  
  24.                     List<int> newNumbers = new List<int>();
  25.  
  26.                     if (index >= 0 && index < numbers.Count)
  27.                     {
  28.                         for (int i = 0; i <= index; i++)
  29.                         {
  30.                             newNumbers.Add(numbers[i]);
  31.                         }
  32.                         numbers.RemoveRange(0, index + 1);
  33.                         numbers.AddRange(newNumbers);
  34.                     }
  35.                     else
  36.                     {
  37.                         Console.WriteLine("Invalid index");
  38.                     }
  39.                 }
  40.                 if (command[0] == "max" || command[0] == "min")
  41.                 {
  42.  
  43.                     if (command[1] == "even")
  44.                     {
  45.                         bool allOdd = numbers.All(x => x % 2 != 0);
  46.  
  47.                         if (allOdd)
  48.                         {
  49.                             Console.WriteLine("No matches");
  50.                             command = Console.ReadLine().Split(' ').ToArray();
  51.                             continue;
  52.                         }
  53.                     }
  54.                     if (command[1] == "odd")
  55.                     {
  56.                         bool allEven = numbers.All(x => x % 2 == 0);
  57.  
  58.                         if (allEven)
  59.                         {
  60.                             Console.WriteLine("No matches");
  61.                             command = Console.ReadLine().Split(' ').ToArray();
  62.                             continue;
  63.                         }
  64.                     }
  65.                     if (command[0] == "max" && command[1] == "even")
  66.                     {
  67.                         indexResult = numbers.Select(x => x).Where(x => x % 2 == 0).OrderBy(x => x).Last();
  68.                     }
  69.                     else if (command[0] == "max" && command[1] == "odd")
  70.                     {
  71.                         indexResult = numbers.Select(x => x).Where(x => x % 2 != 0).OrderBy(x => x).Last();
  72.  
  73.                     }
  74.                     else if (command[0] == "min" && command[1] == "even")
  75.                     {
  76.                         indexResult = numbers.Select(x => x).Where(x => x % 2 == 0).OrderBy(x => x).First();
  77.  
  78.                     }
  79.                     else if (command[0] == "min" && command[1] == "odd")
  80.                     {
  81.                         indexResult = numbers.Select(x => x).Where(x => x % 2 != 0).OrderBy(x => x).First();
  82.                     }
  83.                     int maxIndex = numbers.LastIndexOf(indexResult);
  84.                     Console.WriteLine(maxIndex);
  85.                 }
  86.                 if (command[0] == "first" || command[0] == "last")
  87.                 {
  88.                     var resultList = new List<int>();
  89.                     var count = int.Parse(command[1]);
  90.            
  91.                     if (count > numbers.Count)
  92.                     {
  93.                         Console.WriteLine("Invalid count");
  94.                         command = Console.ReadLine().Split(' ').ToArray();
  95.                         continue;
  96.                     }
  97.                     if (command[0] == "first" && command[2] == "even")
  98.                     {
  99.                         resultList = numbers.Select(x => x).Where(x => x % 2 == 0).Take(count).ToList();
  100.                     }
  101.                     else if (command[0] == "first" && command[2] == "odd")
  102.                     {
  103.                         resultList = numbers.Select(x => x).Where(x => x % 2 != 0).Take(count).ToList();
  104.                     }
  105.                     else if (command[0] == "last" && command[2] == "even")
  106.                     {
  107.                         resultList = numbers.Select(x => x).Where(x => x % 2 == 0).Reverse().Take(count).Reverse().ToList();
  108.                     }
  109.                     else if (command[0] == "last" && command[2] == "odd")
  110.                     {
  111.                         resultList = numbers.Select(x => x).Where(x => x % 2 != 0).Reverse().Take(count).Reverse().ToList();
  112.                     }
  113.                     Console.WriteLine("[" + string.Join(", ", resultList) + "]");
  114.                 }
  115.  
  116.                 command = Console.ReadLine().Split(' ').ToArray();
  117.             }
  118.  
  119.             Console.WriteLine("[" + string.Join(", ", numbers) + "]");
  120.         }
  121.  
  122.        
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement