Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _02.AnonymousThreat
- {
- public class StartUp
- {
- public static void Main()
- {
- List<string> input = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .ToList();
- StringBuilder sb = new StringBuilder();
- while (true)
- {
- string command = Console.ReadLine();
- if (command == "3:1")
- {
- break;
- }
- else
- {
- var commandArgs = command.Split(' ');
- if (command.Contains("merge"))
- {
- int startIndex = int.Parse(commandArgs[1]);
- int endIndex = int.Parse(commandArgs[2]);
- MergeStrings(input, sb, startIndex, endIndex);
- }
- else if (command.Contains("divide"))
- {
- int index = int.Parse(commandArgs[1]);
- int partitions = int.Parse(commandArgs[2]);
- Divide(index, partitions, input, sb);
- }
- }
- }
- Console.WriteLine(string.Join(" ", input));
- }
- private static void Divide(int index, int partitions, List<string> input, StringBuilder sb)
- {
- if (partitions > input[index].Length)
- {
- partitions = input[index].Length;
- }
- if (partitions == 0 || partitions == 1)
- {
- return;
- }
- int singlePartLength = input[index].Length / partitions;
- List<char> temp = input[index].ToCharArray().ToList();
- List<string> reversed = new List<string>();
- input.RemoveAt(index);
- for (int i = 0; i < partitions - 1; i++)
- {
- for (int j = 0, k = 0; j < singlePartLength; j++)
- {
- sb.Append(temp[k].ToString());
- temp.Remove(temp[k]);
- }
- reversed.Add(sb.ToString());
- sb.Append(" ");
- sb.Clear();
- }
- sb.Append(string.Join("", temp));
- reversed.Add(sb.ToString());
- reversed.Reverse();
- for (int i = 0; i < reversed.Count; i++)
- {
- input.Insert(index, reversed[i]);
- }
- sb.Clear();
- }
- private static void MergeStrings(List<string> input, StringBuilder sb, int startIndex, int endIndex)
- {
- if ((startIndex < 0 && endIndex <= 0) || (startIndex >= input.Count && endIndex >= input.Count))
- {
- return;
- }
- else if (startIndex <= 0 && endIndex >= 0 && endIndex < input.Count)
- {
- startIndex = 0;
- }
- else if (startIndex >= 0 && startIndex < input.Count && endIndex >= input.Count)
- {
- endIndex = input.Count - 1;
- }
- else if (startIndex < 0 && endIndex >= input.Count)
- {
- startIndex = 0;
- endIndex = input.Count - 1;
- }
- for (int i = startIndex; i <= endIndex; i++)
- {
- sb.Append(input[i]);
- }
- input.RemoveRange(startIndex, endIndex - startIndex + 1);
- input.Insert(startIndex, sb.ToString());
- sb.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment