JulianJulianov

The Final Quest

Jun 20th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. The Final Quest
  2. 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.
  3. 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:
  4. -   Delete {index} – removes the word after the given index if it is valid.
  5. -   Swap {word1} {word2} – find the given words in the collections if they exist and swap their places.
  6. -   Put {word} {index}add a word at the previous place {index} before the
  7. given one, if it is valid. Note: putting at the last index simply appends the word to the end of the list.
  8. -   Sort – you must sort the words in descending order.
  9. -   Replace {word1} {word2} – find the second word {word2} in the collection (if it exists) and replace it with the first word – {word1}.
  10. 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.
  11. Input / Constraints
  12. • On the 1st line, you are going to receive the collection of words, split by a single space – strings
  13. • On the next lines, you are going to receive commands, until you receive the "Stop" command
  14. Output
  15. • Print the words you have gathered on a single line, split by a single space
  16. Examples
  17. Input                                                              Output
  18. Congratulations! You last also through the have challenge!         Congratulations! You made it through the last challenge!
  19. Swap have last
  20. Replace made have
  21. Delete 2
  22. Put it 8
  23. Stop   
  24. Comments
  25. 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:
  26. Congratulations! You have also through the last challenge!
  27. 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.
  28. Input                                                               Output
  29. This the my quest! final                                            This is the final quest!
  30. Put is 2
  31. Swap final quest!
  32. Delete 2
  33. Stop
  34.  
  35. using System;
  36. using System.Linq;
  37.  
  38. namespace _03._The_Final_Quest
  39. {
  40.     class Program
  41.     {
  42.         static void Main(string[] args)
  43.         {
  44.             var listOfWords = Console.ReadLine().Split().ToList();
  45.                                                                        
  46.             var instructions = "";
  47.             while (!(instructions = Console.ReadLine()).Equals("Stop"))
  48.             {
  49.                 var command = instructions.Split();
  50.                 if (command[0] == "Delete" && listOfWords.Count > int.Parse(command[1]) + 1 && int.Parse(command[1]) + 1 >= 0)
  51.                 {
  52.                     var index = int.Parse(command[1]) + 1;
  53.                     listOfWords.RemoveAt(index);
  54.                 }
  55.                 else if (command[0] == "Swap" && listOfWords.Contains(command[1]) && listOfWords.Contains(command[2]))
  56.                 {
  57.                     var index1 = listOfWords.IndexOf(command[1]);//Задължително първо и двата индекса се откриват в списъка, защото
  58.                     var index2 = listOfWords.IndexOf(command[2]);//иначе има вероятност да не се разменят местата на думите!
  59.                   //listOfWords[index1] = command[2];//Моя начин
  60.                   //listOfWords[index2] = command[1];
  61.                     listOfWords.RemoveAt(index1);
  62.                     listOfWords.Insert(index1, command[2]);
  63.               //Ако var index2 = listOfWords.IndexOf(command[2]); е на този ред има вероятност да не се извърши размяната!
  64.                     listOfWords.RemoveAt(index2);//Пример: ако Swap e last have,а не have last от Example 1.
  65.                     listOfWords.Insert(index2, command[1]);  
  66.                 }
  67.                 else if (command[0] == "Put" && listOfWords.Count >= int.Parse(command[2]) - 1 && int.Parse(command[2]) - 1 > 0)
  68.                 {
  69.                     var index = int.Parse(command[2]) - 1;
  70.                     listOfWords.Insert(index, command[1]);
  71.                 }
  72.                 else if (command[0] == "Sort")
  73.                 {
  74.                   //listOfWords.Sort();//Моя начин
  75.                   //listOfWords.Reverse();
  76.                     listOfWords.Sort((x, y) => y.CompareTo(x));// Сравнявайки ги един с друг стринговете се сортират в Descending order!
  77.                 }
  78.                 else if (command[0] == "Replace" && listOfWords.Contains(command[2]))
  79.                 {
  80.                     var index = listOfWords.IndexOf(command[2]);
  81.                   //listOfWords[index] = command[1];Моя начин
  82.                     listOfWords.RemoveAt(index);
  83.                     listOfWords.Insert(index, command[1]);
  84.                 }
  85.             }
  86.             Console.WriteLine(string.Join(" ", listOfWords));
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment