08. Anonymous Thread
I have 90/100 in judge but I can't resolve why. I found some issues while debugging but nothing helped.The issue is on test 9. Can somebody help me with that problem. In my opinion the problem should be in Divide method but I am not quite sure.
Here is my code:
List<string> inputLine = (Console.ReadLine() + "").Split().ToList();
string[] command = (Console.ReadLine() + "").Split().ToArray();
while (command[0] != "3:1")
{
switch (command[0])
{
case "merge":
inputLine = Merge(inputLine, Convert.ToInt32(command[1]), Convert.ToInt32(command[2]));
break;
case "divide":
inputLine = Divide(inputLine, Convert.ToInt32(command[1]), Convert.ToInt32(command[2]));
break;
}
command = (Console.ReadLine() + "").Split().ToArray();
}
Console.WriteLine(string.Join(' ', inputLine));
static List<string> Merge(List<string> inputLine, int startIndex, int endIndex)
{
if (startIndex >= inputLine.Count || endIndex < 0) return inputLine;
if (startIndex < 0) startIndex = 0;
if (endIndex >= inputLine.Count) endIndex = inputLine.Count - 1;
string mergedElements = "";
for (int i = startIndex; i <= endIndex; i++)
{
mergedElements += inputLine[startIndex];
inputLine.RemoveAt(startIndex);
}
inputLine.Insert(startIndex, mergedElements);
return inputLine;
}
static List<string> Divide(List<string> inputLine, int index, int partitions)
{
if (partitions > inputLine[index].Length) return inputLine;
int partsSize = (int) Math.Floor((decimal) inputLine[index].Length / partitions);
bool remainder = inputLine[index].Length % partitions != 0;
List<string> newlyCreatedTexts = new();
List<char> wordChars = inputLine[index].ToCharArray().ToList();
int partitionsLeft = partitions;
int currentIndex = 0;
while (partitionsLeft > 0)
{
string part = "";
for (int i = currentIndex; i < partsSize + currentIndex; i++)
{
part += wordChars[0];
wordChars.RemoveAt(0);
if (partitionsLeft == 1 && remainder) foreach (char c in wordChars) part += c;
}
newlyCreatedTexts.Add(part);
currentIndex += partsSize;
partitionsLeft--;
}
inputLine.InsertRange(index, newlyCreatedTexts);
inputLine.RemoveAt(index + partitions);
return inputLine;
}
Thanks in advance!