Advertisement
Grimmjow1

AnonymousThreat

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