Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _11._Array_Manipulator
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] numbers = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- string input = string.Empty;
- string[] command;
- while (input != "end")
- {
- input = Console.ReadLine();
- if (input == "end")
- {
- Console.Write($"[{String.Join(", ", numbers)}]");
- break;
- }
- command = input.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray();
- if (command[0] == "exchange")
- {
- int n = int.Parse(command[1]);
- if (int.Parse(command[1]) > numbers.Length)
- {
- Console.WriteLine("Invalid index");
- }
- else
- {
- numbers = RotateArrayByGivenN(numbers, n);
- }
- }
- if (command[0] == "max")
- {
- PrintTheIndexOfTheMaxOddOrEvenNumberInArray(numbers, command[1]);
- }
- if (command[0] == "min")
- {
- PrintTheIndexOfTheMinOddOrEvenNumberInArray(numbers, command[1]);
- }
- if (command[0] == "first")
- {
- PrintTheFirstNOddOrEvenNumbersInArray(numbers, int.Parse(command[1]), command[2]);
- }
- if (command[0] == "last")
- {
- PrintTheLastNOddOrEvenNumbersInArray(numbers, int.Parse(command[1]), command[2]);
- }
- }
- }
- static int[] RotateArrayByGivenN(int[] numbers, int num)
- {
- int[] newarr = new int[numbers.Length];
- int n = num + 1;
- for (int i = 0; i < numbers.Length; i++)
- {
- if (n>=numbers.Length)
- {
- n = 0;
- }
- newarr[i] = numbers[n];
- n++;
- }
- return newarr;
- }
- static void PrintTheIndexOfTheMaxOddOrEvenNumberInArray(int[] numbers, string n)
- {
- int maxNumber= int.MinValue;
- int maxIndex = 0;
- if (n == "odd")
- {
- foreach (var item in numbers)
- {
- if (item%2 != 0)
- {
- if (item>maxNumber)
- {
- maxNumber = item;
- maxIndex = Array.IndexOf(numbers, item);
- }
- }
- }
- if (maxNumber > int.MinValue)
- {
- Console.WriteLine(maxIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- if (n == "even")
- {
- foreach (var item in numbers)
- {
- if (item % 2 == 0)
- {
- if (item > maxNumber)
- {
- maxNumber = item;
- maxIndex = Array.IndexOf(numbers, item);
- }
- }
- }
- if (maxNumber > int.MinValue)
- {
- Console.WriteLine(maxIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- }
- static void PrintTheIndexOfTheMinOddOrEvenNumberInArray(int[] numbers, string n)
- {
- int minNumber = int.MaxValue;
- int maxIndex = 0;
- if (n == "odd")
- {
- foreach (var item in numbers)
- {
- if (item % 2 != 0)
- {
- if (item < minNumber)
- {
- minNumber = item;
- maxIndex = Array.IndexOf(numbers, item);
- }
- }
- }
- if (minNumber < int.MaxValue)
- {
- Console.WriteLine(maxIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- if (n == "even")
- {
- foreach (var item in numbers)
- {
- if (item % 2 == 0)
- {
- if (item < minNumber)
- {
- minNumber = item;
- maxIndex = Array.IndexOf(numbers, item);
- }
- }
- }
- if (minNumber < int.MaxValue)
- {
- Console.WriteLine(maxIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- }
- static void PrintTheFirstNOddOrEvenNumbersInArray(int[] numbers, int n, string oddEven)
- {
- int count = 0;
- List<int> sequence = new List<int>();
- if (oddEven == "odd")
- {
- for (int i = 0; i < numbers.Length; i++)
- {
- if (numbers[i]%2!=0)
- {
- sequence.Add(numbers[i]);
- count++;
- if (count==n)
- {
- break;
- }
- }
- }
- }
- if (oddEven == "even")
- {
- for (int i = 0; i < numbers.Length; i++)
- {
- if (numbers[i] % 2 == 0)
- {
- sequence.Add(numbers[i]);
- count++;
- if (count == n)
- {
- break;
- }
- }
- }
- }
- if (n > numbers.Length)
- {
- Console.WriteLine("Invalid count");
- }
- else
- { Console.WriteLine($"[{String.Join(", ", sequence)}]"); }
- }
- static void PrintTheLastNOddOrEvenNumbersInArray(int[] numbers, int n, string oddEven)
- {
- int count = 0;
- List<int> sequence = new List<int>();
- if (oddEven == "odd")
- {
- for (int i = numbers.Length-1; i >= 0; i--)
- {
- if (numbers[i] % 2 != 0)
- {
- sequence.Add(numbers[i]);
- count++;
- if (count == n)
- {
- break;
- }
- }
- }
- }
- if (oddEven == "even")
- {
- for (int i = numbers.Length - 1; i >= 0; i--)
- {
- if (numbers[i] % 2 == 0)
- {
- sequence.Add(numbers[i]);
- count++;
- if (count == n)
- {
- break;
- }
- }
- }
- }
- if (n > numbers.Length)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- sequence.Reverse();
- Console.WriteLine($"[{String.Join(", ", sequence)}]");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment