Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem 2. On the Way to Annapurna
- 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.
- 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:
- • "Add->{Store}->{Item}"
- o Add the store and the item in your diary. If the store already exists, add just the item.
- • "Add->{Store}->{Item},{Item1}…,{ItemN}"
- o Add the store and the items to your notes. If the store already exists in the diary – add just the items to it.
- • "Remove->{Store}"
- o Remove the store and its items from your diary, if it exists.
- 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:
- Stores list:
- {Store}
- <<{Item}>>
- <<{Item}>>
- <<{Item}>>
- Input / Constraints
- • You will be receiving information until the “END” command is given.
- • There will always be at least one store in the diary.
- • Input will always be valid, there is no need to check it explicitly.
- Output
- • Print the list of stores in the format given above.
- Examples
- Input Output
- Add->PeakSports->Map,Navigation,Compass Stores list:
- Add->Paragon->Sunscreen PeakSports
- Add->Groceries->Dried-fruit,Nuts <<Map>>
- Add->Groceries->Nuts <<Navigation>>
- Add->Paragon->Tent <<Compass>>
- Remove->Paragon Groceries
- Add->Pharmacy->Pain-killers <<Dried-fruit>>
- END <<Nuts>>
- <<Nuts>>
- Pharmacy
- <<Pain-killers>>
- Comments
- 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.
- Add->Peak->Waterproof,Umbrella Stores list:
- Add->Groceries->Water,Juice,Food Peak
- Add->Peak->Tent <<Waterproof>>
- Add->Peak->Sleeping-Bag <<Umbrella>>
- Add->Peak->Jacket <<Tent>>
- Add->Groceries->Lighter <<Sleeping-Bag>>
- Remove->Groceries <<Jacket>>
- Remove->Store
- END
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace OnTheWayToAnnapurna
- {
- class Program
- {
- static void Main(string[] args)
- {
- var listStores = new Dictionary<string, List<string>>();
- var input = "";
- while (!(input = Console.ReadLine()).Equals("END"))
- {
- var wordSplit = input.Split("->");
- var command = wordSplit[0];
- var store = wordSplit[1];
- switch (command)
- {
- case "Add":
- var items = wordSplit[2].Split(',');
- if (!listStores.ContainsKey(store))
- {
- listStores.Add(store, new List<string>());
- }
- for (int i = 0; i < items.Length; i++)
- {
- listStores[store].Add(items[i]);
- }
- break;
- case "Remove":
- if (listStores.ContainsKey(store))
- {
- listStores.Remove(store);
- }
- break;
- }
- }
- Console.WriteLine("Stores list:");
- foreach (var store in listStores.OrderByDescending(x => x.Value.Count).ThenByDescending(x => x.Key))
- {
- Console.WriteLine(store.Key);
- foreach (var item in store.Value)
- {
- Console.WriteLine($"<<{item}>>");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment