JulianJulianov

List() is the best for IndexOf() and others!

May 31st, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.30 KB | None | 0 0
  1. Last Stop
  2. The group has reached Paris and went to visit "La Louvre". They accidently found a map behind "The Wedding at Canna" painting. It had some instructions, so they have decided to follow them and see where they will lead them. Your job is to help them.
  3. Create a program that follows instructions in order to fulfil a quest. First, you will receive a collection of numbers – each representing a painting number. After that, you are going to be receiving instructions, until the "END" command is given.
  4. -   Change {paintingNumber} {changedNumber} – find the painting with the first number in the collection (if it exists) and change its number with the second number – {changedNumber}.
  5. -   Hide {paintingNumber} – find the painting with this value and if it exists and hide it (remove it).
  6. -   Switch {paintingNumber} {paintingNumber2} – find the given paintings in the collections if they exist and switch their places.
  7. -   Insert {place} {paintingNumber} – insert the painting (paintingNumber) on the next place after the given one, if it exists.
  8. -   Reverse – you must reverse the order of the paintings.
  9. Once you complete the instructions, print the numbers of the paintings on a single line, split by a space.
  10. Input / Constraints
  11. • On the 1st line, you are going to receive the numbers of the paintings, split by a single space – integer numbers in the range [11000]
  12. • On the next lines, you are going to receive commands, until you receive the "END" command
  13. Output
  14. • Print the message you have received after the conversion of all numbers on a single line
  15. Examples
  16. Input                             Output                          Comments
  17. 115 115 101 114 73 111 116 75     70 114 111 116 114 101 115 115  The first command is "Insert". You have to insert painting number 114
  18. Insert 5 114                                                      at the next index after the 5th:115 115 101 114 73 111 114 116 75
  19. Switch 116 73                                                     The "Switch" will switch number 116 with 73 and the collection
  20. Hide 75                                                           should look like this: 115 115 101 114 116  111 114 73  75
  21. Reverse                                                           After receiving the"Hide" command, you must remove 75.
  22. Change 73 70                                                      After that you receive "Reverse" and you have to reverse the whole
  23. Insert 10 85                                                      collection.  By receiving "Change" you have to exchange the value 73
  24. END                                                               with the value70. The next "Insert"command is invalid,
  25.                                                                   because there is no 11th index in the collection.
  26.  
  27.  
  28. Input                                                     Output   
  29. 77 120 115 101 101 97 78 88 112 111 108 101 111 110       77 117 115 101 101 78 32 97 112 111 108 101 111 110      
  30. Insert 5 32
  31. Switch 97 78
  32. Hide 88
  33. Change 120 117
  34. END
  35.  
  36. using System;
  37. using System.Linq;
  38.  
  39. public class Program
  40. {
  41.     public static void Main()
  42.     {
  43.              var numbersPaintings = Console.ReadLine().Split().Select(int.Parse).ToList();
  44.  
  45.             var input = "";
  46.             while (!(input = Console.ReadLine()).Equals("END"))
  47.             {
  48.                 var command = input.Split();
  49.                 switch (command[0])
  50.                 {
  51.                     case "Change":
  52.                         if (numbersPaintings.Contains(int.Parse(command[1])))
  53.                         {
  54.                             var firstIndex = numbersPaintings.IndexOf(int.Parse(command[1]));
  55.                             numbersPaintings.RemoveAt(firstIndex);
  56.                             numbersPaintings.Insert(firstIndex, int.Parse(command[2]));
  57.                         }
  58.                         break;
  59.                     case "Hide":
  60.                         if (numbersPaintings.Contains(int.Parse(command[1])))
  61.                         {
  62.                             numbersPaintings.Remove(int.Parse(command[1]));
  63.                         }
  64.                         break;
  65.                     case "Switch":
  66.                         if (numbersPaintings.Contains(int.Parse(command[1])) && numbersPaintings.Contains(int.Parse(command[2])))
  67.                         {   //Чудесен заместител на Replace()!
  68.                             var index1 = numbersPaintings.IndexOf(int.Parse(command[1]));
  69.                             numbersPaintings[index1] = int.Parse(command[2]);
  70.                             var index2 = numbersPaintings.IndexOf(int.Parse(command[2]));
  71.                             numbersPaintings[index2] = int.Parse(command[1]);
  72.                         }
  73.                         break;
  74.                     case "Insert":
  75.                         var place = int.Parse(command[1]) + 1;
  76.                         if (numbersPaintings.Count >= place && place >= 0)
  77.                         {
  78.                             numbersPaintings.Insert(place, int.Parse(command[2]));
  79.                         }
  80.                        
  81.                         break;
  82.                     case "Reverse":
  83.                         numbersPaintings.Reverse();
  84.                         break;
  85.                 }
  86.             }
  87.             Console.WriteLine(string.Join(" ", numbersPaintings));
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment