JulianJulianov

Mid Exam03 - 16 April 2019

Aug 2nd, 2020 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.17 KB | None | 0 0
  1. Problem 3. Easter Shopping
  2. You have decided to go on an Easter shopping spree to take advantage of the promotions.
  3. Create a program that helps you keep track of the shops that you want to visit. You will receive the list of shops you have planned on checking out on a single line, separated by a single space in the following format:
  4. "{shop1} {shop2} {shop3}… {shopn}"
  5. Then you will receive a number – n - a count of commands you need to execute over your list. There are four possible commands:
  6. "Include {shop}":
  7. o   Add the shop at the end of your list.
  8. "Visit {first/last} {numberOfShops}"
  9. o   Remove either the "first" or the "last" number of shops from your list, depending on the input. If you have less shops on your list than the given number, skip this command.
  10. "Prefer {shopIndex1} {shopIndex2}":
  11. o   If both of the shop indexes exist in your list, take the shops that are on them and change their places.
  12. "Place {shop} {shopIndex}"
  13. o   Insert the shop after the given index, only if the resulted index exists.
  14. In the end print the manipulated list in the following format:
  15. "Shops left:
  16. {shop1} {shop2}… {shopn}"
  17. Input / Constraints
  18. • On the 1st line, you will receive the starting list with the names of the shops separated by a single space.
  19. • On the 2nd line, you will receive the number of commands - n – an integer in range [1100]
  20. • On the next n lines you will be receiving commands in the format described above.
  21. Output
  22. • Print the list after the manipulations in the format described above.
  23. Examples
  24. Input                                                                  Output
  25. Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore      Shops left:
  26. 5                                                                      ThriftShop ToyStore Groceries Library Armani PeakStore
  27. Include HM
  28. Visit first 2
  29. Visit last 1
  30. Prefer 3 1
  31. Place Library 2
  32.    
  33. Comments
  34. First we receive the "Include" and the name of the store and we add the store to our list. The list should look like this: Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore HM
  35. After, we receive the "Visit" command and "first", which means we have to visit the first 2 stores, so we remove them from our list and the collection should look like this: ThriftShop Armani Groceries ToyStore PeakStore HM. After that, we receive the "Visit" command again, but this time we need to visit the "last" 1 store, so we remove it and the collection should look like this: ThriftShop Armani Groceries ToyStore PeakStore. After that we receive the "Prefer" command, which means we need to find the shop on the first given index – 3 and change it with the one that is on index – 1, and the collection should look like this: ThriftShop ToyStore Groceries Armani PeakStore. At last, we receive the "Place" command and we need to insert the shop at the next index after 2. And our final list looks like this:
  36. ThriftShop ToyStore Groceries Library Armani PeakStore
  37.  
  38.  
  39. Boutique Flowers CandyStore ThriftShop Versace Groceries ToyStore PeakStore      Shops left:
  40. 6                                                                                Flowers Boutique CandyStore ShoeAquarium ThriftShop
  41. Visit first 9
  42. Visit last 4
  43. Prefer 3 8
  44. Prefer 0 1
  45. Place Store 7
  46. Place ShoeAquarium 2
  47.  
  48. using System;
  49. using System.Linq;
  50.  
  51. namespace MidExam16April2019
  52. {
  53.     class Program
  54.     {
  55.         static void Main(string[] args)
  56.         {
  57.             var listOfShops = Console.ReadLine().Split().ToList();
  58.             var numCommands = int.Parse(Console.ReadLine());
  59.  
  60.             for (int i = 1; i <= numCommands; i++)
  61.             {
  62.                 var commands = Console.ReadLine().Split();
  63.  
  64.                 switch (commands[0])
  65.                 {
  66.                     case "Include":
  67.                         listOfShops.Add(commands[1]);
  68.                         break;
  69.                     case "Visit":
  70.                         var numShops = int.Parse(commands[2]);
  71.                         if (numShops <= listOfShops.Count)
  72.                         {
  73.                             if (commands[1] == "first")
  74.                             {
  75.                                 for (int number = 1; number <= numShops; number++)
  76.                                 {
  77.                                     listOfShops.RemoveAt(0);
  78.                                 }                
  79.                             }
  80.                             else
  81.                             {
  82.                                 for (int num = 1; num <= numShops; num++)
  83.                                 {
  84.                                     listOfShops.RemoveAt(listOfShops.Count - 1);
  85.                                 }  
  86.                             }
  87.                         }
  88.                         break;
  89.                     case "Prefer":
  90.                         var index1 = int.Parse(commands[1]);
  91.                         var index2 = int.Parse(commands[2]);
  92.                         if (index1 >= 0 && index1 < listOfShops.Count && index2 >= 0 && index2 < listOfShops.Count && index1 != index2)
  93.                         {
  94.                             var save1Shop = listOfShops[index1];
  95.                             var save2Shop = listOfShops[index2];
  96.                             listOfShops[index1] = save2Shop;
  97.                             listOfShops[index2] = save1Shop;
  98.                             //listOfShops.RemoveAt(index1);
  99.                             //listOfShops.Insert(index1, save2Shop);
  100.                             //listOfShops.RemoveAt(index2);
  101.                             //listOfShops.Insert(index2, save1Shop);
  102.                         }
  103.                         break;
  104.                     case "Place":
  105.                         var index = int.Parse(commands[2]);
  106.                         if (index >= 0 && index < listOfShops.Count)
  107.                         {
  108.                             listOfShops.Insert(index + 1, commands[1]);
  109.                         }
  110.                         break;
  111.                 }
  112.             }
  113.             Console.WriteLine($"Shops left:\n{string.Join(' ', listOfShops)}");
  114.             //Console.WriteLine("Shops left:");
  115.             //Console.WriteLine(string.Join(" ", listOfShops));
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment