Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Numerics;
- namespace ManipulatingArrays
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
- string input = Console.ReadLine();
- while (input != "end")
- {
- ManipulateArray(array, input);
- input = Console.ReadLine();
- }
- PrintArray(array);
- }
- //Determine the command and call the necessary method
- public static void ManipulateArray(int[] array, string instructions)
- {
- //Check the first word of the instructions
- string command = instructions.Split().ToArray()[0];
- if (command == "exchange")
- {
- int index = int.Parse(instructions.Split().ToArray()[1]);
- array = Exchange(array, index);
- }
- else if (command == "max" || command == "min")
- {
- string typeOfDigit = instructions.Split().ToArray()[1];
- MaxMinEvenOdd(array, command, typeOfDigit);
- }
- else
- {
- string evenOdd = instructions.Split().ToArray()[2];
- int count = int.Parse(instructions.Split().ToArray()[1]);
- FirstLastEvenOdd(array, command, count, evenOdd);
- }
- }
- public static void FirstLastEvenOdd(int[] array, string firstLast, int count, string evenOdd)
- {
- if (count > array.Length)
- {
- Console.WriteLine("Invalid count");
- return;
- }
- if (evenOdd == "even")
- {
- PrintFirstLastEven(array, firstLast, count);
- }
- else
- {
- PrintFirstLastOdd(array, firstLast, count);
- }
- }
- public static void PrintFirstLastEven(int[] array, string firstLast, int count)
- {
- if (firstLast == "first")
- {
- int[] result = array.Where(x => x % 2 == 0).Take(count).ToArray();
- Console.WriteLine("[" + string.Join(", ", result) + "]");
- }
- else
- {
- int[] result = array.Where(x => x % 2 == 0).Reverse().Take(count).Reverse().ToArray();
- Console.WriteLine("[" + string.Join(", ", result) + "]");
- }
- }
- public static void PrintFirstLastOdd(int[] array, string firstLast, int count)
- {
- if (firstLast == "first")
- {
- int[] result = array.Where(x => x % 2 != 0).Take(count).ToArray();
- Console.WriteLine("[" + string.Join(", ", result) + "]");
- }
- else
- {
- int[] result = array.Where(x => x % 2 != 0).Reverse().Take(count).Reverse().ToArray();
- Console.WriteLine("[" + string.Join(", ", result) + "]");
- }
- }
- public static void MaxMinEvenOdd(int[] array, string minMax, string typeOfDigit)
- {
- if (typeOfDigit == "even")
- {
- PrintMinMaxEvenIndex(array, minMax);
- }
- else
- {
- PrintMinMaxOddIndex(array, minMax);
- }
- }
- public static void PrintMinMaxEvenIndex(int[] array, string minMax)
- {
- int index = 0;
- int[] evens = array.Where(x => x % 2 == 0).ToArray();
- if (evens.Count() == 0)
- {
- Console.WriteLine("No matches");
- return;
- }
- if (minMax == "min")
- {
- index = Array.LastIndexOf(array, evens.Min());
- }
- else
- {
- index = Array.LastIndexOf(array, evens.Max());
- }
- Console.WriteLine(index);
- }
- public static void PrintMinMaxOddIndex(int[] array, string minMax)
- {
- int index = 0;
- int[] odds = array.Where(x => x % 2 != 0).ToArray();
- if (odds.Count() == 0)
- {
- Console.WriteLine("No matches");
- return;
- }
- if (minMax == "min")
- {
- index = Array.LastIndexOf(array, odds.Min());
- }
- else
- {
- index = Array.LastIndexOf(array, odds.Max());
- }
- Console.WriteLine(index);
- }
- public static int[] Exchange(int[] array, int index)
- {
- if (index >= array.Length || index < 0) { Console.WriteLine("Invalid index"); return array; }
- int[] first = array.Take(index + 1).ToArray();
- int[] second = array.Skip(index + 1).ToArray();
- List<int> exchanged = new List<int>(second);
- exchanged.AddRange(first);
- return exchanged.ToArray();
- }
- public static void PrintArray(int[] array)
- {
- Console.WriteLine("[" + string.Join(", ", array) + "]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement