SimeonTs

SUPyF2 Lists-Advanced-Exercise - 09. On the Way to Annapurna

Oct 10th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. """
  2. Lists Advanced - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1731#7
  4.  
  5. SUPyF2 Lists-Advanced-Exercise - 09. On the Way to Annapurna (not included in final score)
  6.  
  7. Problem:
  8. You’ve hired a Sherpa and he has a list of supplies you both need to go on the way.
  9. He has passed you some notes and you have to order them correctly in a
  10. diary before you start circling around the town’s stores.
  11.  
  12. Create a program, that lists stores and the items that can be found in them.
  13. You are going to be receiving commands with the information you need until you get the "End" command.
  14. There are three possible commands:
  15. • "Add->{Store}->{Item}"
  16. o   Add the store and the item in your diary. If the store already exists, add just the item.
  17. • "Add->{Store}->{Item},{Item1}…,{ItemN}"
  18. o   Add the store and the items to your notes. If the store already exists in the diary – add just the items to it.
  19. • "Remove->{Store}"
  20. o   Remove the store and its items from your diary, if it exists.
  21. In the end, print the collection sorted by the count of the items in descending order
  22. and then by the names of the stores, again, in descending order in the following format:
  23. Stores list:
  24. {Store}
  25. <<{Item}>>
  26. <<{Item}>>
  27. <<{Item}>>
  28. Input / Constraints
  29. • You will be receiving information until the “END” command is given.
  30. • There will always be at least one store in the diary.
  31. • Input will always be valid, there is no need to check it explicitly.
  32. Output
  33. • Print the list of stores in the format given above.
  34.  
  35. Examples:
  36. Input:
  37. Add->PeakSports->Map,Navigation,Compass
  38. Add->Paragon->Sunscreen
  39. Add->Groceries->Dried-fruit,Nuts
  40. Add->Groceries->Nuts
  41. Add->Paragon->Tent
  42. Remove->Paragon
  43. Add->Pharmacy->Pain-killers
  44. END
  45.  
  46. Output:
  47. Stores list:
  48. PeakSports
  49. <<Map>>
  50. <<Navigation>>
  51. <<Compass>>
  52. Groceries
  53. <<Dried-fruit>>
  54. <<Nuts>>
  55. <<Nuts>>
  56. Pharmacy
  57. <<Pain-killers>>
  58.  
  59. Comments:
  60. First, we receive the "Add" command with a couple of items and we have to add the store and the items to.
  61. We keep doing that for each line of input and when we receive the "Remove" command,
  62. we delete the store and its items from our records.
  63. In the end we print the stores sorted by the count of their items and then by their names.
  64. """
  65. shops = []
  66. while True:
  67.     data = input().split("->")
  68.     if data[0] == "END":
  69.         break
  70.     if data[0] == "Add":
  71.         shop_in_list = False
  72.         for shop in shops:
  73.             if shop[0] == data[1]:
  74.                 shop += [item for item in data[2].split(",")]
  75.                 shop_in_list = True
  76.                 break
  77.         if not shop_in_list:
  78.             shops += [[data[1]] + [item for item in data[2].split(",")]]
  79.     elif data[0] == "Remove":
  80.         for shop in shops:
  81.             if shop[0] == data[1]:
  82.                 shops.remove(shop)
  83.  
  84. shops = sorted(sorted(shops, key=lambda x: x[0], reverse=True), key=lambda x: len(x), reverse=True)
  85.  
  86. print(f"Stores list:")
  87. for shop in shops:
  88.     print(shop[0])
  89.     for item in shop[1:]:
  90.         print(f"<<{item}>>")
Add Comment
Please, Sign In to add comment