Advertisement
Guest User

8. *Anonymous Threat

a guest
Jun 14th, 2019
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Anonymous_threat_3
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<string> code = Console.ReadLine().Split().ToList();
  11.             string[] command = Console.ReadLine().Split(' ').ToArray();
  12.             while (command[0]!="3:1")
  13.             {
  14.                 int input1 = int.Parse(command[1]);
  15.                 int input2 = int.Parse(command[2]);
  16.                 if (command[0] == "merge")
  17.                 {
  18.                     merge(code, input1, input2);
  19.                 }
  20.                 else
  21.                 {
  22.                     divide(code, input1, input2);
  23.                 }
  24.  
  25.                 command = Console.ReadLine().Split(' ').ToArray();
  26.             }
  27.             Console.WriteLine(string.Join(" ", code));
  28.         }
  29.  
  30.         static void divide(List<string> code, int index, int partitions)
  31.         {
  32.             if (index< 0)
  33.             {
  34.                 index = 0;
  35.             }
  36.            
  37.             List<string>dividing = new List<string>();
  38.             string word = code[index];
  39.             if (index > word.Length-1)
  40.             {
  41.                 index = word.Length - 1;
  42.             }
  43.             int counterTemporary = 0;
  44.             char[] wordChars = new char[word.Length];
  45.             if (wordChars.Length<partitions)
  46.             {
  47.                 return;
  48.             }
  49.             for (int i = 0; i < word.Length; i++)
  50.             {
  51.                 wordChars[i] = word[i];
  52.             }
  53.             if (wordChars.Length % partitions==0)
  54.             {
  55.                 string[] temporary = new string[wordChars.Length / partitions];
  56.                 for (int i = 0; i < wordChars.Length; i++)
  57.                 {
  58.                     temporary[counterTemporary] = wordChars[i].ToString();
  59.                     if (((i + 1) % (wordChars.Length / partitions)) == 0)
  60.                     {
  61.                         dividing.Add(string.Join("", temporary));
  62.                         counterTemporary = 0;
  63.                     }
  64.                     else
  65.                     {
  66.                         counterTemporary++;
  67.                     }
  68.                 }
  69.             }
  70.             else if (wordChars.Length%partitions!=0)
  71.             {
  72.                 for (int i = 0; i < partitions-1; i++)
  73.                 {
  74.                     dividing.Add(wordChars[i].ToString());
  75.                     counterTemporary++;
  76.                 }
  77.                 string[] temporary = new string[wordChars.Length - counterTemporary];
  78.                 for (int i = counterTemporary; i < wordChars.Length; i++)
  79.                 {
  80.                     temporary[i-counterTemporary] = wordChars[i].ToString();
  81.                     if (i == wordChars.Length-1)
  82.                     {
  83.                         dividing.Add(string.Join("", temporary));
  84.                     }
  85.                 }
  86.             }
  87.             code.RemoveAt(index);
  88.             code.InsertRange(index, dividing);
  89.         }
  90.        
  91.         static void merge(List<string> code, int input1, int input2)
  92.         {
  93.             if (input1 < 0)
  94.             {
  95.                 input1 = 0;
  96.             }
  97.             if (input2 > code.Count -1)
  98.             {
  99.                 input2 = code.Count-1;
  100.             }
  101.             if (input2 < 0)
  102.             {
  103.                 input2 = 0;
  104.             }
  105.             if (input1>=input2)
  106.             {
  107.                 return;
  108.             }
  109.             string[] merging = new string[input2 - input1+1];
  110.             for (int i = input1; i <= input2; i++)
  111.             {
  112.                 merging[i - input1] = code[input1];
  113.                 code.RemoveAt(input1);
  114.             }
  115.             string merged = string.Join("", merging);
  116.             code.InsertRange(input1, merged.Split().ToList());
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement