C# 04. Mixed Up List - More Exercises
Колеги, дава ми 50/100. Пробвах с различни примерни стойности - на конзолата ми дава верни отговори.
Условието на задачата е следното:
Write a program that mixes up two lists by some rules. You will receive two lines of input, each one
being a list of numbers. The rules for mixing are:
- Start from the beginning of the first list and from the ending of the second
- Add element from the first and element from the second
- At the end there will always be a list in which there are 2 elements remaining
- These elements will be the range of the elements you need to print
- Loop through the result list and take only the elements that fulfill the condition
- Print the elements ordered in ascending order and separated by a space
Example
Input :
1 5 23 64 2 3 34 54 12
43 23 12 31 54 51 92
Output: 23 23 31 34 43 51
Comment
After looping through the two of the arrays we get:
1 92 5 51 23 54 64 31 2 12 3 23 34 43
The constrains are 54 and 12 (so we take only the numbers between them):
51 23 31 23 34 43
We print the result sorted
Кодът ми е следния:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<int> firstInput = Console.ReadLine()
.Split(new[]{' ', ','}, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
List<int> secondInput = Console.ReadLine()
.Split(new[]{' ', ','}, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
List<int> mixed = MakeMixedList(firstInput, secondInput);
int minBorder = CheckRigthListAndMin(firstInput, secondInput);
int maxBorder = CheckRigthListAndMax(firstInput, secondInput);
List<int> rezult = MakeRezultList(mixed, minBorder, maxBorder);
Console.WriteLine(string.Join(" ", rezult));
}
public static int CheckRigthListAndMin(List<int> firstInput, List<int> secondInput)
{
if(firstInput.Count > secondInput.Count)
{
return Math.Min(firstInput[firstInput.Count -1], firstInput[firstInput.Count -2]);
}
else
{
return Math.Min(secondInput[secondInput.Count -1], secondInput[secondInput.Count -2]);
}
}
public static int CheckRigthListAndMax(List<int> firstInput, List<int> secondInput)
{
if(firstInput.Count > secondInput.Count)
{
return Math.Max(firstInput[firstInput.Count -1], firstInput[firstInput.Count -2]);
}
else
{
return Math.Max(secondInput[secondInput.Count -1], secondInput[secondInput.Count -2]);
}
}
public static List<int> MakeMixedList(List<int> firstInput, List<int> secondInput)
{
List<int> mixed = new List<int>();
if (firstInput.Count > secondInput.Count)
{
secondInput.Reverse();
for (int i = 0; i < secondInput.Count; i++)
{
mixed.Add(firstInput[i]);
mixed.Add(secondInput[i]);
}
} // end if
else
{
firstInput.Reverse();
for (int i = 0; i < firstInput.Count; i++)
{
mixed.Add(secondInput[i]);
mixed.Add(firstInput[i]);
}
} // end else
return mixed;
}
public static List <int> MakeRezultList(List<int> mixed, int minBorder, int maxBorder)
{
List<int> rezult = mixed
.Where(x => (minBorder < x && x < maxBorder))
.OrderBy(x => x)
.ToList();
return rezult;
}
}