JulianJulianov

On the Way to Annapurna

Jul 14th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. Problem 2. On the Way to Annapurna
  2. You’ve hired a Sherpa and he has a list of supplies you both need to go on the way. He has passed you some notes and you have to order them correctly in a diary before you start circling around the town’s stores.
  3.  
  4. Create a program, that lists stores and the items that can be found in them. You are going to be receiving commands with the information you need until you get the "End" command. There are three possible commands:
  5. "Add->{Store}->{Item}"
  6. o   Add the store and the item in your diary. If the store already exists, add just the item.
  7. "Add->{Store}->{Item},{Item1}…,{ItemN}"
  8. o   Add the store and the items to your notes. If the store already exists in the diary – add just the items to it.
  9. "Remove->{Store}"
  10. o   Remove the store and its items from your diary, if it exists.
  11. In the end, print the collection sorted by the count of the items in descending order and then by the names of the stores, again, in descending order in the following format:
  12. Stores list:
  13. {Store}
  14. <<{Item}>>
  15. <<{Item}>>
  16. <<{Item}>>
  17. Input / Constraints
  18. • You will be receiving information until the “END” command is given.
  19. • There will always be at least one store in the diary.
  20. • Input will always be valid, there is no need to check it explicitly.
  21. Output
  22. • Print the list of stores in the format given above.
  23. Examples
  24. Input                                                  Output
  25. Add->PeakSports->Map,Navigation,Compass                Stores list:
  26. Add->Paragon->Sunscreen                                PeakSports
  27. Add->Groceries->Dried-fruit,Nuts                       <<Map>>
  28. Add->Groceries->Nuts                                   <<Navigation>>
  29. Add->Paragon->Tent                                     <<Compass>>
  30. Remove->Paragon                                        Groceries
  31. Add->Pharmacy->Pain-killers                            <<Dried-fruit>>
  32. END                                                    <<Nuts>>
  33.                                                        <<Nuts>>
  34.                                                        Pharmacy
  35.                                                        <<Pain-killers>>
  36. Comments
  37. First, we receive the "Add" command with a couple of items and we have to add the store and the items to. We keep doing that for each line of input and when we receive the "Remove" command, we delete the store and its items from our records. In the end we print the stores sorted by the count of their items and then by their names.
  38.  
  39. Add->Peak->Waterproof,Umbrella                         Stores list:
  40. Add->Groceries->Water,Juice,Food                       Peak
  41. Add->Peak->Tent                                        <<Waterproof>>
  42. Add->Peak->Sleeping-Bag                                <<Umbrella>>
  43. Add->Peak->Jacket                                      <<Tent>>
  44. Add->Groceries->Lighter                                <<Sleeping-Bag>>
  45. Remove->Groceries                                      <<Jacket>>
  46. Remove->Store
  47. END
  48.  
  49. using System;
  50. using System.Collections.Generic;
  51. using System.Linq;
  52.  
  53. namespace OnTheWayToAnnapurna
  54. {
  55.     class Program
  56.     {
  57.         static void Main(string[] args)
  58.         {
  59.             var listStores = new Dictionary<string, List<string>>();
  60.             var input = "";
  61.             while (!(input = Console.ReadLine()).Equals("END"))
  62.             {
  63.                 var wordSplit = input.Split("->");
  64.                 var command = wordSplit[0];
  65.                 var store = wordSplit[1];
  66.                
  67.                 switch (command)
  68.                 {
  69.                     case "Add":
  70.                         var items = wordSplit[2].Split(',');
  71.                         if (!listStores.ContainsKey(store))
  72.                         {
  73.                             listStores.Add(store, new List<string>());
  74.                         }
  75.                         for (int i = 0; i < items.Length; i++)
  76.                         {
  77.                             listStores[store].Add(items[i]);
  78.                         }
  79.                         break;
  80.                     case "Remove":
  81.                         if (listStores.ContainsKey(store))
  82.                         {
  83.                             listStores.Remove(store);
  84.                         }
  85.                         break;
  86.                 }
  87.             }
  88.             Console.WriteLine("Stores list:");
  89.             foreach (var store in listStores.OrderByDescending(x => x.Value.Count).ThenByDescending(x => x.Key))
  90.             {
  91.                 Console.WriteLine(store.Key);
  92.                 foreach (var item in store.Value)
  93.                 {
  94.                     Console.WriteLine($"<<{item}>>");
  95.                 }
  96.             }
  97.         }
  98.     }
  99. }
Add Comment
Please, Sign In to add comment