Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _11ArrayManipulator
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] arrayNumbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- string command = string.Empty;
- while ((command = Console.ReadLine()) != "end")
- {
- string[] tokens = command.Split(" ");
- switch (command)
- {
- case "max even":
- MaxEven(arrayNumbers);
- break;
- case "max odd":
- MaxOdd(arrayNumbers);
- break;
- case "min even":
- MinEven(arrayNumbers);
- break;
- case "min odd":
- MinOdd(arrayNumbers);
- break;
- }
- if (command.StartsWith("first") && command.EndsWith("even"))
- {
- FirstEven(int.Parse(tokens[1]), arrayNumbers);
- }
- else if (command.StartsWith("first") && command.EndsWith("odd"))
- {
- FirstOdd(int.Parse(tokens[1]), arrayNumbers);
- }
- else if (command.StartsWith("last") && command.EndsWith("even"))
- {
- LastEven(int.Parse(tokens[1]), arrayNumbers);
- }
- else if (command.StartsWith("last") && command.EndsWith("odd"))
- {
- LastOdd(int.Parse(tokens[1]), arrayNumbers);
- }
- else if (command.StartsWith("exchange"))
- {
- Exchange(arrayNumbers, int.Parse(tokens[1]));
- }
- }
- Console.WriteLine($"[{string.Join(", ", arrayNumbers)}]");
- }
- private static void Exchange(int[] arrayNumbers, int index)
- {
- if (index > arrayNumbers.Length)
- {
- Console.WriteLine("Invalid index");
- }
- int[] resultArray = new int [arrayNumbers.Length]; // create new array
- for (int i = 0, j = index + 1; j < arrayNumbers.Length; i++, j++) // loop until filling the first part of the array [4, 5]
- {
- resultArray[i] = arrayNumbers[j];
- }
- for (int i = index, j = 0; i < arrayNumbers.Length; i++, j++) // loop until filling the second part of the array [1, 2, 3]
- {
- resultArray[i] = arrayNumbers[j];
- }
- Console.WriteLine($"[{string.Join(", ", resultArray)}]");
- }
- private static void MaxEven(int[] arrayNumbers)
- {
- int index = 0;
- int maxValue = int.MinValue;
- bool isMaxEvenExists = false;
- for (int i = 0; i < arrayNumbers.Length; i++)
- {
- if (arrayNumbers[i] % 2 == 0 && arrayNumbers[i] >= maxValue)
- {
- index = i;
- maxValue = arrayNumbers[i];
- isMaxEvenExists = true;
- }
- }
- if (isMaxEvenExists)
- {
- Console.WriteLine(index);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- private static void MaxOdd(int[] arrayNumbers)
- {
- int index = 0;
- int maxValue = int.MinValue;
- bool isMaxOddExists = false;
- for (int i = 0; i < arrayNumbers.Length; i++)
- {
- if (arrayNumbers[i] % 2 != 0 && arrayNumbers[i] >= maxValue)
- {
- index = i;
- maxValue = arrayNumbers[i];
- isMaxOddExists = true;
- }
- }
- if (isMaxOddExists)
- {
- Console.WriteLine(index);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- private static void MinEven(int[] arrayNumbers)
- {
- int index = 0;
- int minValue = int.MaxValue;
- bool isMinEvenExists = false;
- for (int i = 0; i < arrayNumbers.Length; i++)
- {
- if (arrayNumbers[i] % 2 == 0 && arrayNumbers[i] <= minValue)
- {
- index = i;
- minValue = arrayNumbers[i];
- isMinEvenExists = true;
- }
- }
- if (isMinEvenExists)
- {
- Console.WriteLine(index);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- private static void MinOdd(int[] arrayNumbers)
- {
- int index = 0;
- int minValue = int.MaxValue;
- bool isMinOddExists = false;
- for (int i = 0; i < arrayNumbers.Length; i++)
- {
- if (arrayNumbers[i] % 2 != 0 && arrayNumbers[i] <= minValue)
- {
- index = i;
- minValue = arrayNumbers[i];
- isMinOddExists = true;
- }
- }
- if (isMinOddExists)
- {
- Console.WriteLine(index);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- private static void FirstEven(int count, int[] arrayNumbers)
- {
- int counterOfEvenElements = 0;
- if (count > arrayNumbers.Length)
- {
- Console.WriteLine("Invalid count");
- }
- int[] resultArray = new int[count];
- for (int i = 0; i < arrayNumbers.Length; i++)
- {
- if (arrayNumbers[i] % 2 == 0)
- {
- counterOfEvenElements++;
- resultArray[i] += arrayNumbers[i];
- if (counterOfEvenElements == count)
- {
- break;
- }
- }
- }
- if (counterOfEvenElements == 0)
- {
- Console.WriteLine("[]");
- }
- else
- {
- Console.WriteLine($"[{string.Join(", ", resultArray)}]");
- }
- }
- private static void FirstOdd(int count, int[] arrayNumbers)
- {
- int counterOfOddElements = 0;
- if (count > arrayNumbers.Length)
- {
- Console.WriteLine("Invalid count");
- }
- int[] resultArray = new int[count];
- for (int i = 0; i < arrayNumbers.Length; i++)
- {
- if (arrayNumbers[i] % 2 != 0)
- {
- counterOfOddElements++;
- resultArray[i] += arrayNumbers[i];
- if (counterOfOddElements == count)
- {
- break;
- }
- }
- }
- if (counterOfOddElements == 0)
- {
- Console.WriteLine("[]");
- }
- else
- {
- Console.WriteLine($"[{string.Join(", ", resultArray)}]");
- }
- }
- private static void LastEven(int count, int[] arrayNumbers)
- {
- int counterOfEvenElements = 0;
- if (count > arrayNumbers.Length)
- {
- Console.WriteLine("Invalid count");
- }
- int[] resultArray = new int[count];
- for (int i = arrayNumbers.Length - 1; i >= 0; i--)
- {
- if (arrayNumbers[i] % 2 == 0)
- {
- resultArray[i] += arrayNumbers[i];
- counterOfEvenElements++;
- }
- }
- if (counterOfEvenElements == 0)
- {
- Console.WriteLine("[]");
- }
- else
- {
- Console.WriteLine($"[{string.Join(", ", resultArray)}]");
- }
- }
- private static void LastOdd(int count, int[] arrayNumbers)
- {
- int counterOfOddElements = 0;
- if (count > arrayNumbers.Length)
- {
- Console.WriteLine("Invalid count");
- }
- int[] resultArray = new int[count];
- for (int i = arrayNumbers.Length - 1; i >= 0; i--)
- {
- if (arrayNumbers[i] % 2 != 0)
- {
- resultArray[i] += arrayNumbers[i];
- counterOfOddElements++;
- }
- }
- if (counterOfOddElements == 0)
- {
- Console.WriteLine("[]");
- }
- else
- {
- Console.WriteLine($"[{string.Join(", ", resultArray)}]");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment