Advertisement
Guest User

Anonymous_Threat

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