Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Problem_01.ArrayManipulator
- {
- internal class ArrayManipulator
- {
- private static void Main(string[] args)
- {
- string input = Console.ReadLine();
- int[] numbers = input.Split().Select(int.Parse).ToArray();
- // 1 3 5 7 9
- //exchange 1
- //max odd
- //min even
- //first 2 odd
- //last 2 even
- //exchange 3
- //end
- input = Console.ReadLine();
- while (input != "end")
- {
- // input = Console.ReadLine();
- string[] allCommands = input.Split();
- string command = allCommands[0];
- string subCommand;
- int count = 0;
- switch (command)
- {
- case "exchange":
- {
- int index = int.Parse(allCommands[1]);
- Exchange(numbers, index);
- break;
- }
- case "max":
- subCommand = allCommands[1];
- MaxEvenOdd(numbers, subCommand);
- break;
- case "min":
- {
- subCommand = allCommands[1];
- MinEvenOdd(numbers, subCommand);
- break;
- }
- case "first":
- {
- subCommand = allCommands[2];
- count = int.Parse(allCommands[1]);
- FirstElements(numbers, count, subCommand);
- break;
- }
- case "last":
- {
- subCommand = allCommands[2];
- count = int.Parse(allCommands[1]);
- LastElements(numbers, count, subCommand);
- break;
- }
- }
- input = Console.ReadLine();
- }
- Console.Write("[");
- Console.Write(string.Join(", ", numbers));
- Console.WriteLine("]");
- }
- private static void LastElements(int[] numbers, int count, string subCommand)
- {
- int[] tempArr = new int[numbers.Length];
- for (int i = 0; i < numbers.Length; i++)
- {
- tempArr[i] = numbers[i];
- }
- Array.Reverse(tempArr);
- if (count > numbers.Length || count <= 0)
- {
- Console.WriteLine("Invalid count");
- return;
- }
- if (subCommand == "even")
- {
- tempArr = tempArr.Where(x => x % 2 == 0).Take(count).ToArray();
- }
- else
- {
- tempArr = numbers.Where(x => x % 2 != 0).Take(count).ToArray();
- }
- Console.Write("[");
- if (tempArr.Length > 0)
- {
- Console.Write(string.Join(", ", tempArr));
- }
- Console.WriteLine("]");
- }
- private static void FirstElements(int[] numbers, int count, string subCommand)
- {
- int[] tempArr = new int[numbers.Length];
- if (count > numbers.Length || count <= 0)
- {
- Console.WriteLine("Invalid count");
- return;
- }
- if (subCommand == "even")
- {
- tempArr = numbers.Where(x => x % 2 == 0).Take(count).ToArray();
- }
- else
- {
- tempArr = numbers.Where(x => x % 2 != 0).Take(count).ToArray();
- }
- Console.Write("[");
- if (tempArr.Length > 0)
- {
- Console.Write(string.Join(", ", tempArr));
- }
- Console.WriteLine("]");
- }
- private static void MinEvenOdd(int[] numbers, string subCommand)
- {
- if (subCommand == "even")
- {
- int result = int.MaxValue;
- int index = 0;
- for (int i = 0; i < numbers.Length; i++)
- {
- if (numbers[i] % 2 == 0 &&
- numbers[i] <= result)
- {
- result = numbers[i];
- index = i;
- }
- }
- if (result == int.MaxValue)
- {
- Console.WriteLine("No matches");
- return;
- }
- Console.WriteLine(index);
- }
- else
- {
- int result = int.MaxValue;
- int index = 0;
- for (int i = 0; i < numbers.Length; i++)
- {
- if (numbers[i] % 2 != 0 &&
- numbers[i] <= result)
- {
- result = numbers[i];
- index = i;
- }
- }
- if (result == int.MaxValue)
- {
- Console.WriteLine("No matches");
- return;
- }
- Console.WriteLine(index);
- }
- }
- private static void MaxEvenOdd(int[] numbers, string subCommand)
- {
- if (subCommand == "even")
- {
- int result = int.MinValue;
- int index = 0;
- for (int i = 0; i < numbers.Length; i++)
- {
- if (numbers[i] % 2 == 0 &&
- numbers[i] >= result)
- {
- result = numbers[i];
- index = i;
- }
- }
- if (result == int.MinValue)
- {
- Console.WriteLine("No matches");
- return;
- }
- else
- {
- Console.WriteLine(index);
- return;
- }
- }
- else
- {
- int result = int.MinValue;
- int index = 0;
- for (int i = 0; i < numbers.Length; i++)
- {
- if (numbers[i] % 2 != 0 &&
- numbers[i] >= result)
- {
- result = numbers[i];
- index = i;
- }
- }
- if (result == int.MinValue)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(index);
- }
- }
- }
- private static void Exchange(int[] numbers, int index)
- {
- List<int> result = new List<int>();
- if (index + 1 > numbers.Length || index + 1 <= 0)
- {
- Console.WriteLine("Invalid index");
- return;
- }
- else
- {
- for (int i = index + 1; i < numbers.Length; i++)
- {
- result.Add(numbers[i]);
- }
- for (int i = 0; i <= index; i++)
- {
- result.Add(numbers[i]);
- }
- }
- for (int i = 0; i < numbers.Length; i++)
- {
- numbers[i] = result[i];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement