Advertisement
emilia98

06.StuckZipper

Jun 25th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _06.StuckZipper_2
  6. {
  7.     class StuckZipper_2
  8.     {
  9.         static List<int> list1 = new List<int>();
  10.         static List<int> list2 = new List<int>();
  11.  
  12.         static void Main()
  13.         {
  14.             list1 = GetANewList(list1);
  15.             list2 = GetANewList(list2);
  16.             List<int> zippedList = GetAResult(list1, list2);
  17.             Console.WriteLine(String.Join(" ", zippedList));
  18.         }
  19.  
  20.         static List<int> GetANewList(List<int> list)
  21.         {
  22.             list = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  23.             return list;
  24.         }
  25.  
  26.         static List<int> GetAResult(List<int> list1, List<int> list2)
  27.         {
  28.             //Here, we create a list, which will holds count of digits for each element in our
  29.             //original list. We do the same for the second list.
  30.             List<int> numberDigitsList1 = new List<int>();
  31.             numberDigitsList1 = GetMinDigitsFromAList(list1, numberDigitsList1).Item2;
  32.             int minDigitsList1 = GetMinDigitsFromAList(list1, numberDigitsList1).Item1;
  33.  
  34.             List<int> numberDigitsList2 = new List<int>();
  35.             numberDigitsList2 = GetMinDigitsFromAList(list2, numberDigitsList2).Item2;
  36.             int minDigitsList2 = GetMinDigitsFromAList(list2, numberDigitsList2).Item1;
  37.  
  38.             //Here, we find the count of min digits, so we could search only for numbers with <= count
  39.             // of digits;
  40.             int minDigits = Math.Min(minDigitsList1, minDigitsList2);
  41.  
  42.             //Here, we create two new lists, which will hold only the numbers we want to.
  43.             List<int> newList1 = new List<int>();
  44.             GetANewListWithDigitsConstrains(list1, newList1, numberDigitsList1, minDigits);
  45.             List<int> newList2 = new List<int>();
  46.             GetANewListWithDigitsConstrains(list2, newList2, numberDigitsList2, minDigits);
  47.  
  48.             //Here, we create a list, which will holds the result.
  49.             List<int> zippedList = new List<int>();
  50.             zippedList = CreateAZippedList(newList1, newList2, zippedList);
  51.  
  52.             return zippedList;
  53.         }
  54.  
  55.         /*In this method we will check what is the minimum count of digits in both lists -> list1 & list2
  56.           The first value is the number of minimum count of digits.
  57.           The second value is the list, in which we'll hold the count of digits of each element in
  58.           each lists*/
  59.         static Tuple<int, List<int>> GetMinDigitsFromAList(List<int> list, List<int> numberDigitsList)
  60.         {
  61.             //Because the max numbers we could hold is 1000000 -> 7 digits
  62.             int minDigitsList = 7;
  63.  
  64.             for (int i = 0; i < list.Count; i++)
  65.             {
  66.                 int currentNumber = Math.Abs(list[i]);
  67.                 string currentNumberToString = currentNumber.ToString();
  68.                 int digitsCurrentNumber = currentNumberToString.Length;
  69.  
  70.                 if (digitsCurrentNumber < minDigitsList)
  71.                 {
  72.                     minDigitsList = digitsCurrentNumber;
  73.                 }
  74.  
  75.                 //Add the count of digits of our current element
  76.                 numberDigitsList.Add(digitsCurrentNumber);
  77.             }
  78.             return new Tuple<int, List<int>>(minDigitsList, numberDigitsList);
  79.         }
  80.  
  81.         // Here, we create a new list as removing all the elements from each of input lists, which
  82.         // has more digits than needed
  83.         static List<int> GetANewListWithDigitsConstrains(List<int> list, List<int> newList,
  84.                                                         List<int> numberDigitsList, int minDigits)
  85.         {          
  86.             for (int i = 0; i < list.Count; i++)
  87.             {
  88.                 int currentNumber = list[i];
  89.                 if(numberDigitsList[i] <= minDigits)
  90.                 {
  91.                     newList.Add(currentNumber);
  92.                 }
  93.             }
  94.             return newList;
  95.         }
  96.  
  97.         //Here, we create the list, which we will print
  98.         static List<int> CreateAZippedList(List<int> newList1, List<int> newList2, List<int> zippedList)
  99.         {
  100.             int zippedListCount = newList1.Count + newList2.Count;
  101.             int elementPositionInList1 = 0, elementPositionInList2 = 0;
  102.  
  103.             for (int pos = 0; pos < zippedListCount; pos++)
  104.             {
  105.                 //If we haven't reached the end of each list
  106.                 if (elementPositionInList2 < newList2.Count && elementPositionInList1 < newList1.Count)
  107.                 {
  108.                     if (pos % 2 == 0 && elementPositionInList2 < newList2.Count)
  109.                     {
  110.                         zippedList = ZipInList(newList2, zippedList, elementPositionInList2);
  111.                         elementPositionInList2++;
  112.                     }
  113.                     else if (pos % 2 == 1 && elementPositionInList1 < newList1.Count)
  114.                     {
  115.                         zippedList = ZipInList(newList1, zippedList, elementPositionInList1);
  116.                         elementPositionInList1++;
  117.                     }
  118.                 }
  119.                 //If we have reached the end of the first list, we should continue adding the elements only
  120.                 //from the second one.
  121.                 else if (elementPositionInList2 < newList2.Count && elementPositionInList1 >= newList1.Count)
  122.                 {
  123.                     zippedList = ZipInList(newList2, zippedList, elementPositionInList2);
  124.                     elementPositionInList2++;
  125.                 }
  126.                 //If we have reached the end of the second list, we should continue adding the elements only
  127.                 //from the first one.
  128.                 else if (elementPositionInList2 >= newList2.Count && elementPositionInList1 < newList1.Count)
  129.                 {
  130.                     zippedList = ZipInList(newList1, zippedList, elementPositionInList1);
  131.                     elementPositionInList1++;
  132.                 }
  133.                 //If we have reached the end of both lists, we should exit from the loop.
  134.                 //Maybe, it's not nesessery to have this case, because we will
  135.                 //exit the for-loop nevertheless
  136.                 // -> The number of iterations is sum of elements from the both lists.
  137.                 else
  138.                 {
  139.                     break;
  140.                 }
  141.             }
  142.             return zippedList;
  143.         }
  144.  
  145.         //Here, we add elements to the result list
  146.         static List<int> ZipInList(List<int> newList, List<int> zippedList, int elementPositionInList)
  147.         {
  148.             int elementToZip = newList[elementPositionInList];
  149.             zippedList.Add(elementToZip);
  150.             elementPositionInList++;
  151.             return zippedList;
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement