Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace AnonymousDS
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<string> text = Console.ReadLine().Split().ToList();
  12. List<string> partitions = new List<string>();
  13. string line;
  14. int startIndex;
  15. int endIndex;
  16. while ((line = Console.ReadLine()) != "3:1")
  17. {
  18. string[] input = line.Split();
  19. string command = input[0];
  20. switch (command)
  21. {
  22. case "merge":
  23. startIndex = int.Parse(input[1]);
  24. endIndex = int.Parse(input[2]);
  25. startIndex = ValidIndex(startIndex, text.Count);
  26. endIndex = ValidIndex(endIndex, text.Count);
  27.  
  28. string merged = "";
  29. for (int i = startIndex; i <= endIndex; i++)
  30. {
  31. merged += text[i];
  32. }
  33. for (int i = startIndex; i <= endIndex; i++)
  34. {
  35. text.RemoveAt(startIndex);
  36. }
  37. text.Insert(startIndex, merged);
  38. break;
  39.  
  40. case "divide":
  41. startIndex = int.Parse(input[1]);
  42. endIndex = int.Parse(input[2]);
  43. string word = text[startIndex];
  44.  
  45. partitions = Parts(word, endIndex);
  46. text.RemoveAt(startIndex);
  47. text.InsertRange(startIndex, partitions);
  48. break;
  49. }
  50.  
  51. }
  52.  
  53. Console.WriteLine(string.Join(" ", text));
  54. }
  55. private static int ValidIndex(int index, int length)
  56. {
  57. if (index > length - 1)
  58. {
  59. index = length - 1;
  60. }
  61.  
  62. if (index < 0)
  63. {
  64. index = 0;
  65. }
  66. return index;
  67. }
  68. private static List<string> Parts(string word, int partitionsCount)
  69. {
  70. List<string> result = new List<string>();
  71.  
  72. int partLenght = word.Length / partitionsCount;
  73. for (int i = 0; i < partitionsCount; i++)
  74. {
  75. if (i == partitionsCount - 1)
  76. {
  77. result.Add(word.Substring(i * partLenght));
  78. }
  79. else
  80. {
  81. result.Add(word.Substring(i * partLenght, partLenght));
  82. }
  83. }
  84. return result;
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement