JulianJulianov

Mid Exam02 - 16 April 2019

Jul 24th, 2020 (edited)
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. Problem 2. Easter Gifts
  2. As a good friend, you decide to buy presents for your friends.
  3. Create a program that helps you plan the gifts for your friends and family. First, you are going to receive the gifts you plan on buying оn a single line, separated by space, in the following format:
  4. "{gift1} {gift2} {gift3}… {giftn}"
  5. Then you will start receiving commands until you read the "No Money" message. There are three possible commands:
  6. "OutOfStock {gift}"
  7. o   Find the gifts with this name in your collection, if there are any, and change their values to "None".  
  8. "Required {gift} {index}"
  9. o   Replace the value of the current gift on the given index with this gift, if the index is valid.
  10. "JustInCase {gift}"
  11. o   Replace the value of your last gift with this one.
  12. In the end, print the gifts on a single line, except the ones with value "None", separated by a single space in the following format:
  13. "{gift1} {gift2} {gift3}… {giftn}"
  14. Input / Constraints
  15. • On the 1st line you are going to receive the names of the gifts, separated by a single space.
  16. • On the next lines, until the "No Money" command is received, you will be receiving commands.
  17. • The input will always be valid.
  18. Output
  19. • Print the gifts in the format described above.
  20. Examples
  21. Input                                                            Output
  22. Eggs StuffedAnimal Cozonac Sweets EasterBunny Eggs Clothes       StuffedAnimal Spoon Sweets EasterBunny ChocolateEgg
  23. OutOfStock Eggs
  24. Required Spoon 2
  25. JustInCase ChocolateEgg
  26. No Money   
  27. Comments
  28. First, we receive the command "OutOfStock" and we need to replace the values of "Eggs" with "None".  After this command the list should look like this:
  29. None StuffedAnimal Cozonac Sweets EasterBunny None Clothes.
  30. Afterwards, we receive the "Required" command and we need to replace the value on the 2nd index of our list with the value "Spoon". The list should look like this:  
  31. None StuffedAnimal Spoon Sweets EasterBunny None Clothes
  32. After, we receive the "JustInCase" command, which means we need to replace the last value in our list with "ChocolateEggs". The list should look like this:
  33. None StuffedAnimal Spoon Sweets EasterBunny None ChocolateEggs
  34. In the end, we print all of the gifts, except the ones with values "None". This is the result list:
  35. StuffedAnimal Spoon Sweets EasterBunny ChocolateEggs
  36.  
  37.  
  38. Sweets Cozonac Clothes Flowers Wine Clothes Eggs Clothes          Sweets Cozonac Chocolate Flowers Wine Eggs Hat
  39. Required Paper 8
  40. OutOfStock Clothes
  41. Required Chocolate 2
  42. JustInCase Hat
  43. OutOfStock Cable
  44. No Money   
  45.  
  46. using System;
  47. using System.Linq;
  48.  
  49. namespace _02Retake_Mid_Exam___16_April_2019
  50. {
  51.     class Program
  52.     {
  53.         static void Main(string[] args)
  54.         {
  55.             var giftsBuying = Console.ReadLine().Split().ToList();
  56.             var input = "";
  57.             while (!(input = Console.ReadLine()).Equals("No Money"))
  58.             {
  59.                 var command = input.Split();
  60.                 switch (command[0])
  61.                 {
  62.                     case "OutOfStock":
  63.                         while (giftsBuying.Contains(command[1]))
  64.                         {
  65.                             var indexGift = giftsBuying.IndexOf(command[1]);
  66.                             giftsBuying.RemoveAt(indexGift);
  67.                             giftsBuying.Insert(indexGift, "None");
  68.                         }
  69.                         break;
  70.                     case "Required":
  71.                         var index = int.Parse(command[2]);
  72.                         if (index > -1 && index < giftsBuying.Count)
  73.                         {
  74.                             giftsBuying.RemoveAt(index);
  75.                             giftsBuying.Insert(index, command[1]);
  76.                         }
  77.                         break;
  78.                     case "JustInCase":
  79.                         giftsBuying.RemoveAt(giftsBuying.Count - 1);
  80.                         giftsBuying.Add(command[1]);
  81.                         break;
  82.                 }
  83.             }
  84.             foreach (var gift in giftsBuying)
  85.             {
  86.                 if (gift != "None")
  87.                 {
  88.                     Console.Write($"{gift} ");
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95.  
Add Comment
Please, Sign In to add comment