11. Array Manipulator 70/100
Дава грешка на 3, 6, и 10 тест
using System;
using System.Linq;
namespace _11._Array_Manipulator
{
class Program
{
static void Main(string[] args)
{
int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
string[] command = Console.ReadLine().Split();
while (command[0] != "end")
{
switch (command[0])
{
case "exchange":
int index = int.Parse(command[1]);
numbers = ExchangePartsOfArray(index, numbers);
break;
case "max":
PrintMaxEvenOrOddIndex(command[1], numbers);
break;
case "min":
PrintMinEvenOrOddIndex(command[1], numbers);
break;
case "first":
int count = int.Parse(command[1]);
PrintFirstEvenOrOddDigitByCount(count, command[2], numbers);
break;
case "last":
count = int.Parse(command[1]);
PrintLastEvenOrOddDigitByCount(count, command[2], numbers);
break;
}
command = Console.ReadLine().Split();
}
Console.WriteLine("[" + string.Join(", ", numbers) + "]");
}
static int[] ExchangePartsOfArray(int index, int[] arr)
{
if (index > arr.Length - 1)
{
Console.WriteLine("Invalid index");
return arr;
}
int[] temp = arr.Take(index + 1).ToArray();
Array.Copy(arr, index + 1, arr, 0, arr.Length - index - 1);
Array.Copy(temp, 0, arr, arr.Length - index - 1, temp.Length);
return arr;
}
static void PrintMaxEvenOrOddIndex(string EvenOrOdd, int[] arr)
{
int MaxEven = int.MinValue;
int MaxEvenIndex = -1;
int MaxOdd = int.MinValue;
int MaxOddIndex = -1;
for (int i = 0; i < arr.Length; i++)
{
int currentDigit = arr[i];
if (MaxEven <= currentDigit && currentDigit % 2 == 0)
{
MaxEven = currentDigit;
MaxEvenIndex = i;
}
if (MaxOdd <= currentDigit && currentDigit % 2 == 1)
{
MaxOdd = currentDigit;
MaxOddIndex = i;
}
}
if (EvenOrOdd == "even")
{
if (MaxEvenIndex == -1)
{
Console.WriteLine("No matches");
}
else
{
Console.WriteLine(MaxEvenIndex);
}
}
else
{
if (MaxOddIndex == -1)
{
Console.WriteLine("No matches");
}
else
{
Console.WriteLine(MaxOddIndex);
}
}
}
static void PrintMinEvenOrOddIndex(string EvenOrOdd, int[] arr)
{
int MaxEven = int.MaxValue;
int MaxEvenIndex = -1;
int MaxOdd = int.MaxValue;
int MaxOddIndex = -1;
for (int i = 0; i < arr.Length; i++)
{
int currentDigit = arr[i];
if (MaxEven >= currentDigit && currentDigit % 2 == 0)
{
MaxEven = currentDigit;
MaxEvenIndex = i;
}
if (MaxOdd >= currentDigit && currentDigit % 2 == 1)
{
MaxOdd = currentDigit;
MaxOddIndex = i;
}
}
if (EvenOrOdd == "even")
{
if (MaxEvenIndex == -1)
{
Console.WriteLine("No matches");
}
else
{
Console.WriteLine(MaxEvenIndex);
}
}
else
{
if (MaxOddIndex == -1)
{
Console.WriteLine("No matches");
}
else
{
Console.WriteLine(MaxOddIndex);
}
}
}
static void PrintFirstEvenOrOddDigitByCount(int count, string EvenOrOdd, int[] arr)
{
if (count > arr.Length)
{
Console.WriteLine("Invalid count");
return;
}
string OddNumbers = "";
string EvenNumbers = "";
for (int i = 0; i < arr.Length; i++)
{
int currentDigit = arr[i];
if (currentDigit % 2 == 0)
{
EvenNumbers += currentDigit + " ";
}
else
{
OddNumbers += currentDigit + " ";
}
}
string[] OddNumbersArr = OddNumbers.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
string[] EvenNumbersArr = EvenNumbers.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
if (EvenOrOdd == "even")
{
if(count > EvenNumbersArr.Length)
{
count = EvenNumbersArr.Length;
}
Console.WriteLine("[" + string.Join(", ", EvenNumbersArr, 0, count) + "]");
}
else
{
if (count > OddNumbersArr.Length)
{
count = OddNumbersArr.Length;
}
Console.WriteLine("[" + string.Join(", ", OddNumbersArr, 0, count) + "]");
}
}
static void PrintLastEvenOrOddDigitByCount(int count, string EvenOrOdd, int[] arr)
{
if (count > arr.Length)
{
Console.WriteLine("Invalid count");
return;
}
string OddNumbers = "";
string EvenNumbers = "";
for (int i = 0; i < arr.Length; i++)
{
int currentDigit = arr[i];
if (currentDigit % 2 == 0)
{
EvenNumbers += currentDigit + " ";
}
else
{
OddNumbers += currentDigit + " ";
}
}
string[] OddNumbersArr = OddNumbers.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
string[] EvenNumbersArr = EvenNumbers.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
if (EvenOrOdd == "even")
{
if (count > EvenNumbersArr.Length)
{
count = EvenNumbersArr.Length;
}
Console.WriteLine("[" + string.Join(", ", EvenNumbersArr, EvenNumbersArr.Length - count, count) + "]");
}
else
{
if (count > OddNumbersArr.Length)
{
count = OddNumbersArr.Length;
}
Console.WriteLine("[" + string.Join(", ", OddNumbersArr, OddNumbersArr.Length - count, count) + "]");
}
}
}
}
Здравей! Разгледах кода ти и не разбрах какво означава тази част(метод). Какво е IEnumerable и Concat?
if (index >= 0 && index < lineNumbers.Count)
{
IEnumerable<int> firstPatch = lineNumbers.Take(index + 1);
IEnumerable<int> secondPatch = lineNumbers.Skip(index + 1);
lineNumbers = secondPatch.Concat(firstPatch).ToList();
}
Здравей. IEnumerable е интерфейс за колекции. По-лесно да си следя стойностите при дебъгване го написах така вместо да ползвам var защото търсух къде Judge ми връщаше грешен отговор и съм забравил да го сменя на var.....Sorry. Метода Concat пък ми връща нов IEnumerable списък един до друг от двете променливи и с .ToList() го присвоявам като нов списък, заменяйки стария.