Advertisement
YORDAN2347

AnonymousThreat

Feb 1st, 2021 (edited)
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace _08._Anonymous_Threat
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<string> parts = Console.ReadLine()
  11.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  12.                 .ToList();
  13.  
  14.             while (true)
  15.             {
  16.  
  17.                 string[] input = Console.ReadLine()
  18.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 if (input[0] == "3:1")
  21.                 {
  22.                     break;
  23.                 }
  24.  
  25.                 switch (input[0])
  26.                 {
  27.                     case "merge":
  28.                         int startIndex = int.Parse(input[1]);
  29.                         int endIndex = int.Parse(input[2]);
  30.                         MergeList(parts, startIndex, endIndex);
  31.                         break;
  32.  
  33.                     case "divide":
  34.                         int elementIndex = int.Parse(input[1]);
  35.                         int partstoDivide = int.Parse(input[2]);
  36.                         DivideElement(parts, elementIndex, partstoDivide);
  37.                         break;
  38.                 }
  39.             }
  40.             Console.WriteLine(string.Join(" ", parts));
  41.         }
  42.  
  43.         private static void MergeList(List<string> parts, int startIndex, int endIndex)
  44.         {
  45.             if (startIndex < 0)
  46.             {
  47.                 startIndex = 0;
  48.             }
  49.             if (endIndex >= parts.Count)
  50.             {
  51.                 endIndex = parts.Count - 1;
  52.             }
  53.             if (parts.Count > 1)
  54.             {
  55.                 for (int i = startIndex; i < endIndex; i++)
  56.                 {
  57.                     parts[startIndex] += parts[startIndex + 1];
  58.                     parts.RemoveAt(startIndex + 1);
  59.                 }
  60.             }
  61.         }
  62.  
  63.         private static void DivideElement(List<string> input, int index, int partions)
  64.         {
  65.             string word = input[index];
  66.             input.RemoveAt(index);
  67.  
  68.             int eachLenght = (int)(word.Length / partions);
  69.             List<string> allDivided = new List<string>();
  70.  
  71.             if (word.Length % partions == 0)
  72.             {
  73.                 DivideWithoutRemainder(word, eachLenght, allDivided);
  74.             }
  75.             else
  76.             {
  77.                 DivideWithRemainder(word, eachLenght, allDivided, partions);
  78.             }
  79.  
  80.             allDivided.Reverse();
  81.  
  82.             foreach (string item in allDivided)
  83.             {
  84.                 input.Insert(index, item);
  85.             }
  86.  
  87.         }
  88.  
  89.         private static void DivideWithRemainder(string word,
  90.             int eachLenght,
  91.             List<string> allDivided,
  92.             int partions)
  93.         {
  94.             for (int i = 0; i < (partions - 1) * eachLenght; i += eachLenght)
  95.             {
  96.                 string divided = "";
  97.  
  98.                 for (int j = i; j < i + eachLenght; j++)
  99.                 {
  100.                     divided += word[j];
  101.                 }
  102.  
  103.                 allDivided.Add(divided);
  104.             }
  105.  
  106.             allDivided.Add(word.Substring((partions - 1) * eachLenght));
  107.         }
  108.  
  109.         private static void DivideWithoutRemainder(string word,
  110.             int eachLenght,
  111.             List<string> allDivided)
  112.         {
  113.             for (int i = 0; i < word.Length; i += eachLenght)
  114.             {
  115.                 string divided = "";
  116.  
  117.                 for (int j = i; j < i + eachLenght; j++)
  118.                 {
  119.                     divided += word[j];
  120.                 }
  121.  
  122.                 allDivided.Add(divided);
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement