Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The Final Quest
- After walking through fire, the group has reached the final step of the quest. They have received a list with instructions that will help them resolve the last riddle that will lead them to the truth about the Hunting Games.
- 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! Congratulations! You made it through the last challenge!
- Swap have last
- Replace made have
- Delete 2
- Put it 8
- Stop
- 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 This is the final quest!
- Put is 2
- Swap final quest!
- Delete 2
- Stop
- using System;
- using System.Linq;
- namespace _03._The_Final_Quest
- {
- class Program
- {
- static void Main(string[] args)
- {
- var listOfWords = Console.ReadLine().Split().ToList();
- var instructions = "";
- while (!(instructions = Console.ReadLine()).Equals("Stop"))
- {
- var command = instructions.Split();
- if (command[0] == "Delete" && listOfWords.Count > int.Parse(command[1]) + 1 && int.Parse(command[1]) + 1 >= 0)
- {
- var index = int.Parse(command[1]) + 1;
- listOfWords.RemoveAt(index);
- }
- else if (command[0] == "Swap" && listOfWords.Contains(command[1]) && listOfWords.Contains(command[2]))
- {
- var index1 = listOfWords.IndexOf(command[1]);//Задължително първо и двата индекса се откриват в списъка, защото
- var index2 = listOfWords.IndexOf(command[2]);//иначе има вероятност да не се разменят местата на думите!
- //listOfWords[index1] = command[2];//Моя начин
- //listOfWords[index2] = command[1];
- listOfWords.RemoveAt(index1);
- listOfWords.Insert(index1, command[2]);
- //Ако var index2 = listOfWords.IndexOf(command[2]); е на този ред има вероятност да не се извърши размяната!
- listOfWords.RemoveAt(index2);//Пример: ако Swap e last have,а не have last от Example 1.
- listOfWords.Insert(index2, command[1]);
- }
- else if (command[0] == "Put" && listOfWords.Count >= int.Parse(command[2]) - 1 && int.Parse(command[2]) - 1 > 0)
- {
- var index = int.Parse(command[2]) - 1;
- listOfWords.Insert(index, command[1]);
- }
- else if (command[0] == "Sort")
- {
- //listOfWords.Sort();//Моя начин
- //listOfWords.Reverse();
- listOfWords.Sort((x, y) => y.CompareTo(x));// Сравнявайки ги един с друг стринговете се сортират в Descending order!
- }
- else if (command[0] == "Replace" && listOfWords.Contains(command[2]))
- {
- var index = listOfWords.IndexOf(command[2]);
- //listOfWords[index] = command[1];Моя начин
- listOfWords.RemoveAt(index);
- listOfWords.Insert(index, command[1]);
- }
- }
- Console.WriteLine(string.Join(" ", listOfWords));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment