Advertisement
Guest User

Untitled

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