SimeonTs

SUPyF2 P.-Mid-Exam/16 April 2019 - 02. Easter Gifts

Oct 29th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. """
  2. Technology Fundamentals Retake Mid Exam - 16 April 2019
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1610#1
  4.  
  5. SUPyF2 P.-Mid-Exam/16 April 2019 - 02. Easter Gifts
  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. Examples:
  30. Input:
  31. Eggs StuffedAnimal Cozonac Sweets EasterBunny Eggs Clothes
  32. OutOfStock Eggs
  33. Required Spoon 2
  34. JustInCase ChocolateEgg
  35. No Money
  36.  
  37. Output:
  38. StuffedAnimal Spoon Sweets EasterBunny ChocolateEgg
  39.  
  40. Comments:
  41. First, we receive the command "OutOfStock" and we need to replace the values of "Eggs" with "None".  After this command the list should look like this:
  42. None StuffedAnimal Cozonac Sweets EasterBunny None Clothes.
  43. Afterwards, we receive the "Required" command and we need to replace the value on the 2nd index of our list with the value "Spoon". The list should look like this:
  44. None StuffedAnimal Spoon Sweets EasterBunny None Clothes
  45. After, we receive the "JustInCase" command, which means we need to replace the last value in our list with "ChocolateEggs". The list should look like this:
  46. None StuffedAnimal Spoon Sweets EasterBunny None ChocolateEggs
  47. In the end, we print all of the gifts, except the ones with values "None". This is the result list:
  48. StuffedAnimal Spoon Sweets EasterBunny ChocolateEggs
  49.  
  50. Input:
  51. Sweets Cozonac Clothes Flowers Wine Clothes Eggs Clothes
  52. Required Paper 8
  53. OutOfStock Clothes
  54. Required Chocolate 2
  55. JustInCase Hat
  56. OutOfStock Cable
  57. No Money
  58.  
  59. Output:
  60. Sweets Cozonac Chocolate Flowers Wine Eggs Hat
  61. """
  62. gifts = input().split()
  63.  
  64. while True:
  65.     command = input().split()
  66.     if command[0] == "No":
  67.         print(' '.join(gift for gift in gifts if gift != "None"))
  68.         break
  69.  
  70.     elif command[0] == "OutOfStock":
  71.         gift_to_delete = command[1]
  72.         gifts = ["None" if gift == gift_to_delete else gift for gift in gifts]
  73.  
  74.     elif command[0] == "Required":
  75.         new_gift, index = command[1], int(command[2])
  76.         if 0 <= index < len(gifts):
  77.             gifts[index] = new_gift
  78.  
  79.     elif command[0] == "JustInCase":
  80.         new_last_gift = command[1]
  81.         gifts[-1] = new_last_gift
Add Comment
Please, Sign In to add comment