Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. namespace _04._AnonymousThreat
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. class AnonymousThreat
  9. {
  10. static void Main(string[] args)
  11. {
  12. //Lets get the input - the array of strings. There will be many manipulations so why not
  13. //get it directly as list.
  14. List<string> sensitiveData = Console.ReadLine().Split().ToList();
  15.  
  16. string inputLine = string.Empty;
  17.  
  18. //Another cool way to make a "Read Until" While loop
  19. while ((inputLine = Console.ReadLine()) != "3:1")
  20. {
  21. string[] inputParameters = inputLine.Split();
  22.  
  23. string command = inputParameters[0];
  24.  
  25. //Checking the command
  26. if (command == "merge")
  27. {
  28. int startIndex = int.Parse(inputParameters[1]);
  29. int endIndex = int.Parse(inputParameters[2]);
  30.  
  31. //Passing the List to a method
  32. sensitiveData = Merge(sensitiveData, startIndex, endIndex);
  33. }
  34. else if(command == "divide")
  35. {
  36. int index = int.Parse(inputParameters[1]);
  37. int partitions = int.Parse(inputParameters[2]);
  38.  
  39. //Passing the list to a method
  40. sensitiveData = Divide(sensitiveData, index, partitions);
  41. }
  42. }
  43.  
  44. Console.WriteLine(string.Join(" ", sensitiveData));
  45. }
  46.  
  47. //This method will get an index and the max length of the List
  48. //Then it will change the index if it is outside the boundaries of the array.
  49. private static int ChangeIndex(int index, int maxLength)
  50. {
  51. if (index < 0)
  52. {
  53. index = 0;
  54. }
  55.  
  56. if (index >= maxLength)
  57. {
  58. index = maxLength - 1;
  59. }
  60.  
  61. return index;
  62. }
  63.  
  64. //The Merge method
  65. private static List<string> Merge(List<string> sensitiveData, int startIndex, int endIndex)
  66. {
  67. //Lets validate the indexes, so they will not be outsite the array
  68. startIndex = ChangeIndex(startIndex, sensitiveData.Count);
  69. endIndex = ChangeIndex(endIndex, sensitiveData.Count);
  70.  
  71. //Lets create a new list
  72. List<string> newList = new List<string>();
  73.  
  74. //Lets add everything up until the startIndex in the new list.
  75. for (int i = 0; i < startIndex; i++)
  76. {
  77. newList.Add(sensitiveData[i]);
  78. }
  79.  
  80. //Lets get a StringBuilder, since we will be appending strings (merging)
  81. StringBuilder result = new StringBuilder();
  82.  
  83. //Lets merge the elements we need to merge
  84. for (int i = startIndex; i <= endIndex; i++)
  85. {
  86. result.Append(sensitiveData[i]);
  87. }
  88.  
  89. //Lets add the newly merged element to the new list
  90. newList.Add(result.ToString());
  91.  
  92. //Lets add all other elements after the mergin indexes to the newlist
  93. for (int i = endIndex + 1; i < sensitiveData.Count; i++)
  94. {
  95. newList.Add(sensitiveData[i]);
  96. }
  97.  
  98. //What do we get ?
  99. //A new list with all old elements, except the merged ones, which are a totally new SINGLE element.
  100. return newList;
  101. }
  102.  
  103. //The Divide method
  104. private static List<string> Divide(List<string> sensitiveData, int index, int partitions)
  105. {
  106. //Get the element at that index.
  107. //It was said in the Constraints section that it will always be a valid index
  108. string element = sensitiveData[index];
  109.  
  110. //Lets integer divide the element's length into the partitions to get the minimal length
  111. //the elements should have
  112. int partitionLength = element.Length / partitions;
  113.  
  114. //Lets get a new list of the partitions
  115. List<string> dividedPartitions = new List<string>();
  116.  
  117. //Lets get a loop for all the partitions
  118. for (int i = 0; i < partitions; i++)
  119. {
  120. //If its the last element
  121. if (i == partitions - 1)
  122. {
  123. //Just add everything after it, so that it is the longest one
  124. //In other words, accoarding to the description, we must make the LAST element - the LONGEST
  125. dividedPartitions.Add(element.Substring(i * partitionLength));
  126. }
  127. else
  128. {
  129. //If its not the last element, just add a substring from its index
  130. //and cut "partitionLength" amount of characters
  131. dividedPartitions.Add(element.Substring(i * partitionLength, partitionLength));
  132. }
  133. }
  134.  
  135. //Now whats the logic with the i * partitionLength
  136.  
  137. //EXAMPLE NO.1: We have "abcdef" we want to divide it by 4.
  138. //partitionLength = element.Length (6) / partition (4) = 1 (integer division)
  139. //for (i = 0; i < 4; i++)
  140. //First iteration - i = 0. 0 * 1 = 0. Start index - 0, length - 1, Substring result - "a"
  141. //Second iteration - i = 1. 1 * 1 = 1. Start index - 1, length - 1, Substring result - "b"
  142. //Third iteration - i = 2. 2 * 1 = 2. Start index - 2, length - 1, Substring result - "c"
  143. //Fourth iteration (FINAL ITERATION) - i = 3, 3 * 1 = 3. Start index - 3, length - none, Substring result - "def"
  144.  
  145. //EXAMPLE NO.2: Lets try with a longer string.
  146. //We have "123456789" we want to divide it by 4.
  147. //partitionLength = element.Length (9) / partition (4) = 2 (integer division)
  148. //for (i = 0; i < 4; i++)
  149. //First iteration - i = 0. 0 * 2 = 0. Start index - 0, length - 2, Substring result - "12"
  150. //Second iteration - i = 1. 1 * 2 = 2. Start index - 2, length - 2, Substring result - "34"
  151. //Third iteration - i = 2. 2 * 2 = 4. Start index - 4, length - 2, Substring result - "56"
  152. //Fourth iteration (FINAL ITERATION) - i = 3, 3 * 2 = 6. Start index - 6, length - none, Substring result - "789"
  153.  
  154. //Then lets just remove the old element from the array
  155. sensitiveData.RemoveAt(index);
  156. //Then lets just InsertRange the new partitions at that index
  157. sensitiveData.InsertRange(index, dividedPartitions);
  158.  
  159. //Then we get a list with the new divided data in it.
  160. return sensitiveData;
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement