The Final Quest
Здравейте, получавам 91/100 и не мога да си намеря грешката.
Create a program that follows given instructions. You will receive a collection of words on a single line, split by a single space. They are not what they are supposed to be, so you have to follow the instructions in order to find the real message. You will be receiving commands. Here are the possible ones:
- Delete {index} – removes the word after the given index if it is valid.
- Swap {word1} {word2} – find the given words in the collections if they exist and swap their places.
- Put {word} {index} – add a word at the previous place {index} before the
given one, if it is valid. Note: putting at the last index simply appends the word to the end of the list. - Sort – you must sort the words in descending order.
- Replace {word1} {word2} – find the second word {word2} in the collection (if it exists) and replace it with the first word – {word1}.
Follow them until you receive the "Stop" command. After you have successfully followed the instructions, you must print the words on a single line, split by a space.
Input / Constraints
- On the 1st line, you are going to receive the collection of words, split by a single space – strings
- On the next lines, you are going to receive commands, until you receive the "Stop" command
Output
- Print the words you have gathered on a single line, split by a single space
Examples
Input |
Output |
Congratulations! You last also through the have challenge! Swap have last Replace made have Delete 2 Put it 4 Stop |
Congratulations! You made it through the last challenge! |
Comments |
|
First, we receive the command “Swap”, so we change the positions of the words have and last. The text at this point should look like this: Congratulations! You have also through the last challenge! After that, we receive “Replace” and we have to replace the second word – “have” with the first – “made”. Afterwards we have to delete the word, which is after the second index. And finally, we have to put a word on the previous position before 4. |
Input |
Output |
This the my quest! final Put is 2 Swap final quest! Delete 2 Stop |
This is the final quest! |
using System;
using System.Linq;
using System.Collections.Generic;
namespace The_Final_Quest
{
class Program
{
static void Main(string[] args)
{
List<string> text = Console.ReadLine().Split().ToList();
string command = Console.ReadLine();
while (true)
{
string[] token = command.Split();
if (token[0] == "Stop")
{
break;
}
if (token[0] == "Delete")
{
int index = int.Parse(token[1]);
if (int.Parse(token[1]) >= -1 && int.Parse(token[1]) < text.Count - 1)
{
for (int i = 0; i < text.Count; i++)
{
if (index == i)
{
text.RemoveAt(i + 1);
}
}
}
}
else if (token[0] == "Swap")
{
if(text.Contains(token[1]) && text.Contains(token[2]))
{
for (int i = 0; i < text.Count; i++)
{
if (text[i] == token[1])
{
text[i] = token[2];
}
else if (text[i] == token[2])
{
text[i] = token[1];
}
}
}
}
else if (token[0] == "Put")
{
int index = int.Parse(token[2]);
if (int.Parse(token[2]) > 0 && int.Parse(token[2]) <= text.Count)
{
for (int i = 0; i < text.Count; i++)
{
if (index == text.Count)
{
text.Add(token[1]);
}
if (index == i)
{
text.Insert(i - 1, token[1]);
}
}
}
}
else if (token[0] == "Sort")
{
text.Sort();
text.Reverse();
}
else if (token[0] == "Replace")
{
if (text.Contains(token[2]))
{
for (int i = 0; i < text.Count; i++)
{
if (text[i] == token[2])
{
text[i] = token[1];
}
}
}
}
command = Console.ReadLine();
}
Console.WriteLine(string.Join(" ", text));
}
}
}