Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Numerics;
- namespace exampreparation
- {
- class exam
- {
- static void Main(string[] args)
- {
- List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
- string[] command = Console.ReadLine().Split().ToArray();
- int indexResult = 0;
- while (command[0] != "end")
- {
- if (command[0] == "exchange")
- {
- int index = int.Parse(command[1]);
- List<int> newNumbers = new List<int>();
- if (index >= 0 && index < numbers.Count)
- {
- for (int i = 0; i <= index; i++)
- {
- newNumbers.Add(numbers[i]);
- }
- numbers.RemoveRange(0, index + 1);
- numbers.AddRange(newNumbers);
- }
- else
- {
- Console.WriteLine("Invalid index");
- }
- }
- if (command[0] == "max" || command[0] == "min")
- {
- if (command[1] == "even")
- {
- bool allOdd = numbers.All(x => x % 2 != 0);
- if (allOdd)
- {
- Console.WriteLine("No matches");
- command = Console.ReadLine().Split(' ').ToArray();
- continue;
- }
- }
- if (command[1] == "odd")
- {
- bool allEven = numbers.All(x => x % 2 == 0);
- if (allEven)
- {
- Console.WriteLine("No matches");
- command = Console.ReadLine().Split(' ').ToArray();
- continue;
- }
- }
- if (command[0] == "max" && command[1] == "even")
- {
- indexResult = numbers.Select(x => x).Where(x => x % 2 == 0).OrderBy(x => x).Last();
- }
- else if (command[0] == "max" && command[1] == "odd")
- {
- indexResult = numbers.Select(x => x).Where(x => x % 2 != 0).OrderBy(x => x).Last();
- }
- else if (command[0] == "min" && command[1] == "even")
- {
- indexResult = numbers.Select(x => x).Where(x => x % 2 == 0).OrderBy(x => x).First();
- }
- else if (command[0] == "min" && command[1] == "odd")
- {
- indexResult = numbers.Select(x => x).Where(x => x % 2 != 0).OrderBy(x => x).First();
- }
- int maxIndex = numbers.LastIndexOf(indexResult);
- Console.WriteLine(maxIndex);
- }
- if (command[0] == "first" || command[0] == "last")
- {
- var resultList = new List<int>();
- var count = int.Parse(command[1]);
- if (count > numbers.Count)
- {
- Console.WriteLine("Invalid count");
- command = Console.ReadLine().Split(' ').ToArray();
- continue;
- }
- if (command[0] == "first" && command[2] == "even")
- {
- resultList = numbers.Select(x => x).Where(x => x % 2 == 0).Take(count).ToList();
- }
- else if (command[0] == "first" && command[2] == "odd")
- {
- resultList = numbers.Select(x => x).Where(x => x % 2 != 0).Take(count).ToList();
- }
- else if (command[0] == "last" && command[2] == "even")
- {
- resultList = numbers.Select(x => x).Where(x => x % 2 == 0).Reverse().Take(count).Reverse().ToList();
- }
- else if (command[0] == "last" && command[2] == "odd")
- {
- resultList = numbers.Select(x => x).Where(x => x % 2 != 0).Reverse().Take(count).Reverse().ToList();
- }
- Console.WriteLine("[" + string.Join(", ", resultList) + "]");
- }
- command = Console.ReadLine().Split(' ').ToArray();
- }
- Console.WriteLine("[" + string.Join(", ", numbers) + "]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement