petyaminkova91

Untitled

Nov 1st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _03_TheFinalQuest
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<string> words = Console.ReadLine().Split().ToList();
  12.  
  13. string input = Console.ReadLine();
  14.  
  15. while (input != "Stop")
  16. {
  17. string[] commandParts = input.Split();
  18. string command = commandParts[0];
  19.  
  20. switch (command)
  21. {
  22. case "Delete":
  23. int indexToRemove = int.Parse(commandParts[1]) + 1;
  24. DeleteIndex(words, indexToRemove);
  25. break;
  26. case "Swap":
  27. string firstWord = commandParts[1];
  28. string secondWord = commandParts[2];
  29. SwapWords(words, firstWord, secondWord);
  30. break;
  31. case "Put":
  32. string word = commandParts[1];
  33. int index = int.Parse(commandParts[2]);
  34. AddAWordAtThePreviousIndex(words, word, index);
  35. break;
  36. case "Sort":
  37. words.Sort();
  38. words.Reverse();
  39. break;
  40. case "Replace":
  41. firstWord = commandParts[1];
  42. secondWord = commandParts[2];
  43. ReplaceSecondWordWithFirstWord(words, firstWord, secondWord);
  44. break;
  45. }
  46.  
  47. input = Console.ReadLine();
  48. }
  49.  
  50. Console.WriteLine(String.Join(" ", words));
  51. }
  52.  
  53. static void DeleteIndex(List<string> words, int index)
  54. {
  55. bool isIndexValid = 0 <= index && index < words.Count;
  56.  
  57. if (isIndexValid)
  58. {
  59. words.RemoveAt(index);
  60. }
  61. }
  62.  
  63. static void SwapWords(List<string> words, string firstWord, string SecondWord)
  64. {
  65. bool areWordsExist = words.Contains(firstWord) && words.Contains(SecondWord);
  66.  
  67. if (areWordsExist)
  68. {
  69. int firstWordIndex = words.IndexOf(firstWord);
  70. int secondWordIndex = words.IndexOf(SecondWord);
  71.  
  72. words.Remove(SecondWord);
  73. words.Insert(firstWordIndex, SecondWord);
  74. words.Remove(firstWord);
  75. words.Insert(secondWordIndex, firstWord);
  76. }
  77. }
  78.  
  79. static void AddAWordAtThePreviousIndex(List<string> words, string word, int index)
  80. {
  81. int indexToInsertAt = index - 1;
  82. bool isIndexValid = 0 <= indexToInsertAt && indexToInsertAt <= words.Count;
  83.  
  84. if (isIndexValid)
  85. {
  86. words.Insert(indexToInsertAt, word);
  87. }
  88. }
  89.  
  90. static void ReplaceSecondWordWithFirstWord(List<string> words, string firstWord, string secondWord)
  91. {
  92. if (words.Contains(secondWord))
  93. {
  94. int indexOfTheSecondWord = words.IndexOf(secondWord);
  95. words.Remove(secondWord);
  96. words.Insert(indexOfTheSecondWord, firstWord);
  97. }
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment