Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 11. *Array Manipulator
- Trifon has finally become a junior developer and has received his first task. It’s about manipulating an array of integers. He is not quite happy about it, since he hates manipulating arrays. They are going to pay him a lot of money, though, and he is willing to give somebody half of it if to help him do his job. You, on the other hand, love arrays (and money) so you decide to try your luck.
- The array may be manipulated by one of the following commands
- • exchange {index} – splits the array after the given index, and exchanges the places of the two resulting sub-arrays. E.g. [1, 2, 3, 4, 5] -> exchange 2 -> result: [4, 5, 1, 2, 3]
- o If the index is outside the boundaries of the array, print “Invalid index”
- • max even/odd– returns the INDEX of the max even/odd element -> [1, 4, 8, 2, 3] -> max odd -> print 4
- • min even/odd – returns the INDEX of the min even/odd element -> [1, 4, 8, 2, 3] -> min even > print 3
- o If there are two or more equal min/max elements, return the index of the rightmost one
- o If a min/max even/odd element cannot be found, print “No matches”
- • first {count} even/odd– returns the first {count} elements -> [1, 8, 2, 3] -> first 2 even -> print [8, 2]
- • last {count} even/odd – returns the last {count} elements -> [1, 8, 2, 3] -> last 2 odd -> print [1, 3]
- o If the count is greater than the array length, print “Invalid count”
- o If there are not enough elements to satisfy the count, print as many as you can. If there are zero even/odd elements, print an empty array “[]”
- • end – stop taking input and print the final state of the array
- Input
- • The input data should be read from the console.
- • On the first line, the initial array is received as a line of integers, separated by a single space
- • On the next lines, until the command “end” is received, you will receive the array manipulation commands
- • The input data will always be valid and in the format described. There is no need to check it explicitly.
- Output
- • The output should be printed on the console.
- • On a separate line, print the output of the corresponding command
- • On the last line, print the final array in square brackets with its elements separated by a comma and a space
- • See the examples below to get a better understanding of your task
- Constraints
- • The number of input lines will be in the range [2 … 50].
- • The array elements will be integers in the range [0 … 1000].
- • The number of elements will be in the range [1 .. 50]
- • The split index will be an integer in the range [-231 … 231 – 1]
- • first/last count will be an integer in the range [1 … 231 – 1]
- • There will not be redundant whitespace anywhere in the input
- • Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.
- Examples
- Input Output Input Output Input Output
- 1 3 5 7 9 2 1 10 100 1000 3 1 10 100 1000 [1]
- exchange 1 No matches max even Invalid count exchange 3 [1]
- max odd [5, 7] first 5 even Invalid index first 2 odd [1, 10, 100, 1000]
- min even [] exchange 10 0 last 4 odd
- first 2 odd [3, 5, 7, 9, 1] min odd 2 end
- last 2 even exchange 0 0
- exchange 3 max even [10, 100, 1000, 1]
- end min even
- end
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _11ArrayManipulator
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> numbersList = Console.ReadLine().Split().Select(int.Parse).ToList();
- string command = string.Empty;
- while ((command = Console.ReadLine()) != "end")
- {
- List<string> commandList = command.Split().ToList();
- switch (commandList[0])
- {
- case "exchange":
- int index = int.Parse(commandList[1]);
- if (index >= 0 && index < numbersList.Count)
- { List<int> firstPart = numbersList.Take(index + 1).ToList();//Чрез Take() и Skip() разделям списъка на две
- List<int> secondPart = numbersList.Skip(index + 1).ToList();//части и после с Concat() ги долепям(конкате-
- numbersList = secondPart.Concat(firstPart).ToList(); //нация).
- }
- else
- {
- Console.WriteLine("Invalid index");
- }
- break;
- case "max":
- int maxOdd = int.MinValue;
- int maxEven = int.MinValue;
- for (int i = 0; i < numbersList.Count; i++)
- {
- if (numbersList[i] % 2 != 0 && numbersList[i] >= maxOdd)
- {
- maxOdd = numbersList[i];
- }
- else if (numbersList[i] % 2 == 0 && numbersList[i] >= maxEven)
- {
- maxEven = numbersList[i];
- }
- }
- if (commandList[1] == "odd")
- {
- int maxOddIndex = numbersList.LastIndexOf(maxOdd);
- Console.WriteLine(maxOddIndex);
- }
- else if (commandList[1] == "even")
- {
- int maxEvenIndex = numbersList.LastIndexOf(maxEven);
- Console.WriteLine(maxEvenIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- break;
- case "min":
- int minOdd = int.MaxValue;
- int minEven = int.MaxValue;
- for (int i = 0; i < numbersList.Count; i++)
- {
- if (numbersList[i] % 2 != 0 && numbersList[i] <= minOdd)
- {
- minOdd = numbersList[i];
- }
- else if (numbersList[i] % 2 == 0 && numbersList[i] <= minEven)
- {
- minEven = numbersList[i];
- }
- }
- if (commandList[1] == "odd")
- {
- int minOddIndex = numbersList.LastIndexOf(minOdd);
- if (minOddIndex != -1)
- {
- Console.WriteLine(minOddIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- else if (commandList[1] == "even" )
- {
- int minEvenIndex = numbersList.LastIndexOf(minEven);
- if (minEvenIndex != -1)
- {
- Console.WriteLine(minEvenIndex);
- }
- else
- {
- Console.WriteLine("No matches");
- }
- }
- break;
- case "first":
- int digit = int.Parse(commandList[1]);
- if (digit > numbersList.Count)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- if (commandList[2] == "odd")
- {
- List<int> oddList = numbersList.Where(x => x % 2 != 0).ToList();
- List<int> result = oddList.ToList();
- if (result.Count > digit)
- {
- result = result.Take(digit).ToList();
- }
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- else
- {
- List<int> evenList = numbersList.Where(x => x % 2 == 0).ToList();
- List<int> result = evenList.ToList();
- if (result.Count > digit)
- {
- result = result.Take(digit).ToList();
- }
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- }
- break;
- case "last":
- int digits = int.Parse(commandList[1]);
- if (digits > numbersList.Count)
- {
- Console.WriteLine("Invalid count");
- }
- else
- {
- if (commandList[2] == "odd")
- {
- List<int> oddList = numbersList.Where(x => x % 2 != 0).ToList();
- List<int> result = oddList.ToList();
- if (result.Count > digits)
- {
- result = result.Skip(result.Count - digits).ToList();
- }
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- else
- {
- List<int> evenList = numbersList.Where(x => x % 2 == 0).ToList();
- List<int> result = evenList.ToList();
- if (result.Count > digits)
- {
- result = result.Skip(result.Count - digits).ToList();
- }
- Console.WriteLine($"[{string.Join(", ", result)}]");
- }
- }
- break;
- }
- }
- Console.WriteLine($"[{string.Join(", ", numbersList)}]");
- }
- }
- }
Add Comment
Please, Sign In to add comment