Advertisement
SimeonTs

SUPyF2 P.-Mid-Exam/6 August 2019. - Treasure Hunt

Oct 28th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. """
  2. Programming Fundamentals Mid Exam Retake - 6 August 2019
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1773#1
  4.  
  5. SUPyF2 P.-Mid-Exam/6 August 2019. - Treasure Hunt
  6.  
  7. Problem:
  8. The pirates need to carry a treasure chest safely back to the ship. Looting along the way.
  9. Create a program that manages the state of the treasure chest along the way.
  10. On the first line you will receive the initial loot of the treasure chest,
  11. which is a string of items separated by a '|'.
  12. "{loot1}|{loot2}|{loot3}… {lootn}"
  13. The following lines represent commands until "Yohoho!" which ends the treasure hunt:
  14. • Loot {item1} {item2}…{itemn} – pick up treasure loot along the way.
  15. Insert the items at the beginning of the chest. If an item is already contained don't insert it.
  16. • Drop {index} – remove the loot at the given position and add it at the end of the treasure chest.
  17. If the index is invalid skip the command.
  18. • Steal {count} – someone steals the last count loot items.
  19. If there are less items than the given count remove as much as there are. Print the stolen items separated by ', ':
  20. {item1}, {item2}, {item3} … {itemcount}
  21. In the end output the average treasure gain which is the sum of all treasure items length divided by the count
  22. of all items inside the chest formatted to the second decimal point:
  23. "Average treasure gain: {averageGain} pirate credits."
  24. If the chest is empty print the following message:
  25. "Failed treasure hunt."
  26. Input
  27. • On the 1st line you are going to receive the initial treasure chest (loot separated by '|')
  28. • On the next lines, until "Yohoho!", you will be receiving commands.
  29. Output
  30. • Print the output in the format described above.
  31. Constraints
  32. • The loot items will be strings containing any ASCII code.
  33. • The indexes will be integers in the range [-200…200]
  34. • The count will be an integer in the range [1….100]
  35. Examples:
  36. Input:
  37. Gold|Silver|Bronze|Medallion|Cup
  38. Loot Wood Gold Coins
  39. Loot Silver Pistol
  40. Drop 3
  41. Steal 3
  42. Yohoho!
  43.  
  44. Output:
  45. Medallion, Cup, Gold
  46. Average treasure gain: 5.40 pirate credits.
  47.  
  48. Comments:
  49. The first command "Loot Wood Gold Coins" adds Wood and Coins to the chest but omits Gold since it is already contained.
  50. The chest now has the following items:
  51. Coins Wood Gold Silver Bronze Medallion Cup
  52. The second command adds only Pistol to the chest
  53. The third command "Drop 3" removes the Gold from the chest, but immediately adds it at the end:
  54. Pistol Coins Wood Silver Bronze Medallion Cup Gold
  55. The fourth command "Steal 3" removes the last 3 items Medallion, Cup, Gold from the chest and prints them.
  56. In the end calculate the average treasure gain which is the sum of all
  57. items length Pistol(6) + Coins(5) + Wood(4)  + Silver(6) + Bronze(6) = 27 and divide it by the count 27 / 5 = 5.4
  58. and format it to the second decimal point.
  59.  
  60. Input:
  61. Diamonds|Silver|Shotgun|Gold
  62. Loot Silver Medals Coal
  63. Drop -1
  64. Drop 1
  65. Steal 6
  66. Yohoho!
  67.  
  68. Output:
  69. Coal, Diamonds, Silver, Shotgun, Gold, Medals
  70. Failed treasure hunt.
  71. """
  72. initial_loot = [item for item in input().split("|")]
  73.  
  74. while True:
  75.     command = input().split()
  76.     if command[0] == "Yohoho!":
  77.         break
  78.  
  79.     elif command[0] == "Loot":
  80.         new_items = [item for item in command[1:]]
  81.         for item in new_items:
  82.             if item not in initial_loot:
  83.                 initial_loot.insert(0, item)
  84.  
  85.     elif command[0] == "Drop":
  86.         index = int(command[1])
  87.         if 0 <= index <= (len(initial_loot) - 1):
  88.             item = initial_loot[index]
  89.             initial_loot.pop(index)
  90.             initial_loot.append(item)
  91.  
  92.     elif command[0] == "Steal":
  93.         index = min(int(command[1]), len(initial_loot))
  94.         stolen = initial_loot[-index:]
  95.         for i in range(len(stolen)):
  96.             initial_loot.pop(-1)
  97.         print(", ".join(stolen))
  98.  
  99. if len(initial_loot) > 0:
  100.     average_gain = sum([len(item) for item in initial_loot]) / len(initial_loot)
  101.     print(f"Average treasure gain: {average_gain:.2f} pirate credits.")
  102. else:
  103.     print("Failed treasure hunt.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement