TheBulgarianWolf

Mixed Up Lists

Dec 28th, 2020
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace MixedUpLists
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             /*Write a program that mixes up two lists by some rules. You will receive two lines of input, each one being a list of
  12.            numbers. The rules for mixing are:
  13. -               Start from the beginning of the first list and from the ending of the second.
  14. -               Add element from the first and element from the second.
  15. -               At the end there will always be a list, in which there are 2 elements remaining.
  16. -               These elements will be the range of the elements you need to print.
  17. -               Loop through the result list and take only the elements that fulfill the condition.
  18. -               Print the elements ordered in ascending order and separated by a space.*/
  19.             List<int> firstList = new List<int>();
  20.             List<int> secondList = new List<int>();
  21.             Console.WriteLine("Enter the first line of numbers: ");
  22.             int[] firstNumbers = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  23.             Console.WriteLine("Enter the second line of numbers: ");
  24.             int[] secondNumbers = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  25.             for(int i = 0; i < firstNumbers.Length; i++)
  26.             {
  27.                 firstList.Add(firstNumbers[i]);
  28.             }
  29.            
  30.  
  31.             for(int j = 0;j < secondNumbers.Length; j++)
  32.             {
  33.                 secondList.Add(secondNumbers[j]);
  34.             }
  35.             int iterations = 0;
  36.             int firstLimiter = 0;
  37.             int secondLimiter = 0;
  38.             if(firstList.Count > secondList.Count)
  39.             {
  40.                 iterations = secondList.Count;
  41.                 firstLimiter = firstList[firstList.Count - 1];
  42.                 secondLimiter = firstList[firstList.Count - 2];
  43.                 firstList.RemoveAt(firstList.Count - 1);
  44.                 firstList.RemoveAt(firstList.Count - 1);
  45.             }
  46.             else
  47.             {
  48.                 iterations = firstList.Count;
  49.                 firstLimiter = secondList[secondList.Count - 1];
  50.                 secondLimiter = secondList[secondList.Count - 2];
  51.                 secondList.RemoveAt(secondList.Count - 1);
  52.                 secondList.RemoveAt(secondList.Count - 1);
  53.             }
  54.             List<int> allNumbersList = new List<int>();
  55.             for(int index = 0; index < iterations; index++)
  56.             {
  57.                 allNumbersList.Add(firstList[index]);
  58.                 allNumbersList.Add(secondList[secondList.Count-index-1]);
  59.             }
  60.  
  61.            
  62.            
  63.             int min = secondLimiter;
  64.             int max = firstLimiter;
  65.             if (firstLimiter > secondLimiter)
  66.             {
  67.                 max = firstLimiter;
  68.                 min = secondLimiter;
  69.             }
  70.             else
  71.             {
  72.                 min = firstLimiter;
  73.                 max = secondLimiter;
  74.             }
  75.  
  76.             List<int> finalList = new List<int>();
  77.             for(int h = 0; h < allNumbersList.Count; h++)
  78.             {
  79.                 if(allNumbersList[h] > min && allNumbersList[h] < max)
  80.                 {
  81.                     finalList.Add(allNumbersList[h]);
  82.                    
  83.                 }
  84.             }
  85.  
  86.             allNumbersList.Sort();
  87.             finalList.Sort();
  88.             Console.WriteLine("The final numbers are: ");
  89.             foreach (int number in finalList)
  90.             {
  91.                 Console.Write(number + " ");
  92.             }
  93.             Console.WriteLine(firstLimiter);
  94.             Console.WriteLine(secondLimiter);
  95.            
  96.  
  97.  
  98.         }
  99.     }
  100. }
  101.  
Add Comment
Please, Sign In to add comment