SimeonTs

SUPyF2 Lists Basics Exercise - 07. Easter Gifts

Oct 3rd, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. """
  2. Lists Basics - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1725#6
  4.  
  5. SUPyF2 Lists Basics Exercise - 07. Easter Gifts (not included in final score)
  6.  
  7. Problem:
  8. As a good friend, you decide to buy presents for your friends.
  9. Create a program that helps you plan the gifts for your friends and family.
  10. First, you are going to receive the gifts you plan on buying оn a single line,
  11. separated by space, in the following format:
  12. "{gift1} {gift2} {gift3}… {giftn}"
  13. Then you will start receiving commands until you read the "No Money" message. There are three possible commands:
  14. • "OutOfStock {gift}"
  15. o   Find the gifts with this name in your collection, if there are any, and change their values to "None".
  16. • "Required {gift} {index}"
  17. o   Replace the value of the current gift on the given index with this gift, if the index is valid.
  18. • "JustInCase {gift}"
  19. o   Replace the value of your last gift with this one.
  20. In the end, print the gifts on a single line, except the ones with value "None",
  21. separated by a single space in the following format:
  22. "{gift1} {gift2} {gift3}… {giftn}"
  23. Input / Constraints
  24. • On the 1st line you are going to receive the names of the gifts, separated by a single space.
  25. • On the next lines, until the "No Money" command is received, you will be receiving commands.
  26. • The input will always be valid.
  27. Output
  28. • Print the gifts in the format described above.
  29.  
  30. Examples:
  31.  
  32. Input:
  33. Eggs StuffedAnimal Cozonac Sweets EasterBunny Eggs Clothes
  34. OutOfStock Eggs
  35. Required Spoon 2
  36. JustInCase ChocolateEgg
  37. No Money
  38.  
  39. Output:
  40. Eggs StuffedAnimal Cozonac Sweets EasterBunny Eggs Clothes
  41. OutOfStock Eggs
  42. Required Spoon 2
  43. JustInCase ChocolateEgg
  44. No Money    StuffedAnimal Spoon Sweets EasterBunny ChocolateEgg
  45.  
  46. Comments:
  47. First, we receive the command "OutOfStock" and we need to replace the values of "Eggs" with "None".
  48. After this command the list should look like this:
  49. None StuffedAnimal Cozonac Sweets EasterBunny None Clothes.
  50. Afterwards, we receive the "Required" command and we need to replace the value on the 2nd index of our list with the
  51. value "Spoon". The list should look like this:
  52. None StuffedAnimal Spoon Sweets EasterBunny None Clothes
  53. After, we receive the "JustInCase" command, which means we need to replace the last value in our
  54. list with "ChocolateEggs". The list should look like this:
  55. None StuffedAnimal Spoon Sweets EasterBunny None ChocolateEggs
  56. In the end, we print all of the gifts, except the ones with values "None". This is the result list:
  57. StuffedAnimal Spoon Sweets EasterBunny ChocolateEggs
  58.  
  59. Input:
  60. Sweets Cozonac Clothes Flowers Wine Clothes Eggs Clothes
  61. Required Paper 8
  62. OutOfStock Clothes
  63. Required Chocolate 2
  64. JustInCase Hat
  65. OutOfStock Cable
  66. No Money
  67.  
  68. Output:
  69. Sweets Cozonac Chocolate Flowers Wine Eggs Hat
  70. """
  71. gifts = [gift for gift in input().split()]
  72.  
  73. while True:
  74.     command = input()
  75.     if command == "No Money":
  76.         break
  77.     command = [item for item in command.split()]
  78.     if command[0] == "OutOfStock":
  79.         for gift in gifts:
  80.             if gift == command[1]:
  81.                 index_to_change = gifts.index(gift)
  82.                 gifts.pop(index_to_change)
  83.                 gifts.insert(index_to_change, "None")
  84.     elif command[0] == "Required":
  85.         if 0 <= int(command[2]) <= len(gifts) - 1:
  86.             gifts.pop(int(command[2]))
  87.             gifts.insert(int(command[2]), command[1])
  88.     elif command[0] == "JustInCase":
  89.         gifts.pop(-1)
  90.         gifts.append(command[1])
  91.  
  92. print(" ".join([gift for gift in gifts if gift != "None"]))
Add Comment
Please, Sign In to add comment