Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _08._Anonymous_Threat
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> parts = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .ToList();
- while (true)
- {
- string[] input = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries);
- if (input[0] == "3:1")
- {
- break;
- }
- switch (input[0])
- {
- case "merge":
- int startIndex = int.Parse(input[1]);
- int endIndex = int.Parse(input[2]);
- MergeList(parts, startIndex, endIndex);
- break;
- case "divide":
- int elementIndex = int.Parse(input[1]);
- int partstoDivide = int.Parse(input[2]);
- DivideElement(parts, elementIndex, partstoDivide);
- break;
- }
- }
- Console.WriteLine(string.Join(" ", parts));
- }
- private static void MergeList(List<string> parts, int startIndex, int endIndex)
- {
- if (startIndex < 0)
- {
- startIndex = 0;
- }
- if (endIndex >= parts.Count)
- {
- endIndex = parts.Count - 1;
- }
- if (parts.Count > 1)
- {
- for (int i = startIndex; i < endIndex; i++)
- {
- parts[startIndex] += parts[startIndex + 1];
- parts.RemoveAt(startIndex + 1);
- }
- }
- }
- private static void DivideElement(List<string> input, int index, int partions)
- {
- string word = input[index];
- input.RemoveAt(index);
- int eachLenght = (int)(word.Length / partions);
- List<string> allDivided = new List<string>();
- if (word.Length % partions == 0)
- {
- DivideWithoutRemainder(word, eachLenght, allDivided);
- }
- else
- {
- DivideWithRemainder(word, eachLenght, allDivided, partions);
- }
- allDivided.Reverse();
- foreach (string item in allDivided)
- {
- input.Insert(index, item);
- }
- }
- private static void DivideWithRemainder(string word,
- int eachLenght,
- List<string> allDivided,
- int partions)
- {
- for (int i = 0; i < (partions - 1) * eachLenght; i += eachLenght)
- {
- string divided = "";
- for (int j = i; j < i + eachLenght; j++)
- {
- divided += word[j];
- }
- allDivided.Add(divided);
- }
- allDivided.Add(word.Substring((partions - 1) * eachLenght));
- }
- private static void DivideWithoutRemainder(string word,
- int eachLenght,
- List<string> allDivided)
- {
- for (int i = 0; i < word.Length; i += eachLenght)
- {
- string divided = "";
- for (int j = i; j < i + eachLenght; j++)
- {
- divided += word[j];
- }
- allDivided.Add(divided);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement