Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Linq;
- using System.Collections.Generic;
- namespace AnonimusTreat
- {
- class Program
- {
- static void Main(string[] args)
- {
- var list = Console.ReadLine().Split().ToList();
- string command = string.Empty;
- while ((command = Console.ReadLine()) != "3:1")
- {
- string[] commandArray = command.Split();
- if (commandArray[0] == "merge")
- {
- int startIndex = int.Parse(commandArray[1]);
- int validCurrentStartIndex = Math.Max(0, startIndex);
- int validStartIndex = Math.Min(list.Count - 1, validCurrentStartIndex);
- int endIndex = int.Parse(commandArray[2]);
- int validCurrentEndIndex = Math.Max(0, endIndex);
- int validEndIndex = Math.Min(list.Count - 1, validCurrentEndIndex);
- string stringForMerge = string.Empty;
- int CountToRemove = validEndIndex - validStartIndex+ 1;
- for (int i = validStartIndex; i <= validEndIndex; i++)
- {
- stringForMerge += list[i];
- }
- list.RemoveRange(validStartIndex, CountToRemove);
- list.Insert(validStartIndex, stringForMerge);
- }
- else if (commandArray[0] == "divide")
- {
- int index = int.Parse(commandArray[1]);
- int validCurrentIndex = Math.Max(0, index);
- int validIndex = Math.Min(validCurrentIndex, list.Count - 1);
- int partitions = int.Parse(commandArray[2]);
- string stringForDivide = list[validIndex];
- var sb = new StringBuilder(stringForDivide);
- if (sb.Length % partitions == 0)
- {
- int lengthDivide = sb.Length / partitions;
- int indexDivide = lengthDivide;
- for (int i = 0; i <= sb.Length - lengthDivide; i++)
- {
- if (i == indexDivide)
- {
- sb.Insert(i, " ");
- indexDivide += lengthDivide + 1;
- }
- }
- }
- else if (sb.Length % partitions != 0)
- {
- int lengthDivide = sb.Length / partitions;
- int indexDivide = lengthDivide;
- int lastPartitionLength = sb.Length- (lengthDivide * (partitions-1));
- for (int i = 0; i <= sb.Length - lastPartitionLength; i++)
- {
- if (i == indexDivide)
- {
- sb.Insert(i, " ");
- indexDivide += lengthDivide + 1;
- }
- }
- }
- list.RemoveAt(index);
- list.InsertRange(index, sb.ToString().Split());
- }
- }
- Console.WriteLine(string.Join(" ",list));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement