Advertisement
Guest User

Untitled

a guest
Feb 18th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | Source Code | 0 0
  1.  
  2. List<string> input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries)
  3.     .ToList();
  4.  
  5. string command;
  6.  
  7. while ((command = Console.ReadLine()) != "3:1")
  8. {
  9.     string[] cmd = command.Split(" ", StringSplitOptions.RemoveEmptyEntries)
  10.                     .ToArray();
  11.  
  12.     if (cmd[0] == "merge")
  13.     {
  14.         int start = int.Parse(cmd[1]), end = int.Parse(cmd[2]);
  15.         string temp = "";
  16.  
  17.         if ((start < 0 || start > input.Count - 1) &&
  18.             (end < 0 || end > input.Count - 1))
  19.         {
  20.             continue;
  21.         }
  22.         else if (start < 0)
  23.         {
  24.             start = 0;
  25.         }
  26.         else if (end > input.Count - 1)
  27.         {
  28.             end = input.Count - 1;
  29.         }
  30.  
  31.         for (int i = start; i <= end; i++)
  32.         {
  33.             temp += input[i];
  34.         }
  35.  
  36.         input.RemoveRange(start, end - start + 1);
  37.         input.Insert(start, temp);
  38.     }
  39.     else if (cmd[0] == "divide")
  40.     {
  41.         int idx = int.Parse(cmd[1]), parts = int.Parse(cmd[2]);
  42.  
  43.         string temp = input[idx]; //Chosen string for dividing
  44.         if (parts > temp.Length)
  45.         {
  46.             parts = temp.Length;
  47.         }
  48.         string[] splited = new string[parts]; //result of splitting
  49.  
  50.         int partLength = temp.Length / parts; //symbols per part
  51.         int reminder = temp.Length % parts; //for addition to the last part
  52.  
  53.         for (int i = 0; i < parts; i++)
  54.         {
  55.             for (int j = 0; j < partLength; j++)
  56.             {
  57.                 splited[i] += temp[i * partLength + j];
  58.             }
  59.         }
  60.         int reminderStartIx = parts * partLength;
  61.  
  62.         for (int i = reminderStartIx; i < reminderStartIx + reminder; i++)
  63.         {
  64.             splited[splited.Length - 1] += temp[i];
  65.         }
  66.         input.RemoveAt(idx); //Removing devided element
  67.         input.InsertRange(idx, splited);
  68.     }
  69. }
  70. Console.WriteLine(String.Join(" ", input));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement