Advertisement
SimeonTs

SUPyF2 P.-Mid-Exam/6 August 2019. - Man O War

Oct 28th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.46 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#2
  4.  
  5. SUPyF2 P.-Mid-Exam/6 August 2019. - Man O War
  6.  
  7. Problem:
  8. The pirates encounter a huge Man-O-War at sea.
  9. Create a program that tracks the battle and either chooses a winner or prints a stalemate.
  10. On the first line you will receive the status of the pirate ship,
  11. which is a string representing integer sections separated by '>'.
  12. On the second line you will receive the same type of status, but for the warship:
  13. "{section1}>{section2}>{section3}… {sectionn}"
  14. On the third line you will receive the maximum health capacity a section of the ship can reach.
  15. The following lines represent commands until "Retire":
  16. • Fire {index} {damage} – the pirate ship attacks the warship with the given damage at that section.
  17. Check if the index is valid and if not skip the command. If the section breaks (health <= 0) the warship sinks, print the following and stop the program:
  18. "You won! The enemy ship has sunken."
  19. • Defend {startIndex} {endIndex} {damage} - the warship attacks the pirate ship with the given damage at that range
  20. (indexes are inclusive). Check if both indexes are valid and if not skip the command.
  21. If the section breaks (health <= 0) the pirate ship sinks, print the following and stop the program:
  22. "You lost! The pirate ship has sunken."
  23. • Repair {index} {health} - the crew repairs a section of the pirate ship with the given health.
  24. Check if the index is valid and if not skip the command.
  25. The health of the section cannot exceed the maximum health capacity.
  26. • Status – prints the count of all sections of the pirate ship that need repair soon,
  27. which are all sections that are lower than 20% of the maximum health capacity. Print the following:
  28. "{count} sections need repair."
  29. In the end if a stalemate occurs print the status of both ships,
  30. which is the sum of their individual sections in the following format:
  31. "Pirate ship status: {pirateShipSum}"
  32. "Warship status: {warshipSum}"
  33. Input
  34. • On the 1st line you are going to receive the status of the pirate ship (integers separated by '>')
  35. • On the 2nd line you are going to receive the status of the warship
  36. • On the 3rd line you are going receive the maximum health a section of a ship can reach.
  37. • On the next lines, until "Retire", you will be receiving commands.
  38. Output
  39. • Print the output in the format described above.
  40. Constraints
  41. • The section numbers will be integers in the range [1….1000]
  42. • The indexes will be integers [-200….200]
  43. • The damage will be an integer in the range [1….1000]
  44. • The health will be an integer in the range [1….1000]
  45. Examples:
  46. Input:
  47. 12>13>11>20>66
  48. 12>22>33>44>55>32>18
  49. 70
  50. Fire 2 11
  51. Fire 8 100
  52. Defend 3 6 11
  53. Defend 0 3 5
  54. Repair 1 33
  55. Status
  56. Retire
  57.  
  58. Output:
  59. 12>13>11>20>66
  60. 12>22>33>44>55>32>18
  61. 70
  62. Fire 2 11
  63. Fire 8 100
  64. Defend 3 6 11
  65. Defend 0 3 5
  66. Repair 1 33
  67. Status
  68. Retire
  69.  
  70. Comments:
  71. First, we receive the command "Fire 2 11" and damage the warship at section index 2 which is currently
  72. 33 and after reduction the status of the warship is the following:
  73. 12 22 22 44 55 32 18
  74. The second and third command have invalid indexes, so we skip them.
  75. The fourth command "Defend 0 3 5" damages 4 sections of the pirate ship with 5 which results in the following status:
  76. 7 8 6 15 66
  77. The fifth command "Repair 1 33" repairs the pirate ship section and adds 33 health to the current 8 which results in 41
  78. Only 2 sections of the pirate ship (7 and 6) need repair soon.
  79. In the end there is a stalemate, so we print both ship statuses (sum of all sections).
  80.  
  81. Input:
  82. 2>3>4>5>2
  83. 6>7>8>9>10>11
  84. 20
  85. Status
  86. Fire 2 3
  87. Defend 0 4 11
  88. Repair 3 18
  89. Retire
  90.  
  91. Output:
  92. 3 sections need repair.
  93. You lost! The pirate ship has sunken.
  94. """
  95. ship = [int(section) for section in input().split(">")]
  96. warship = [int(section) for section in input().split(">")]
  97. maximum_health_capacity = int(input())
  98.  
  99. while True:
  100.     command = input().split()
  101.     if command[0] == "Retire":
  102.         break
  103.  
  104.     elif command[0] == "Fire":
  105.         index, damage = int(command[1]), int(command[2])
  106.         if 0 <= index < len(warship):
  107.             if (warship[index] - damage) <= 0:
  108.                 print("You won! The enemy ship has sunken.")
  109.                 exit(0)
  110.             else:
  111.                 warship[index] -= damage
  112.  
  113.     elif command[0] == "Defend":
  114.         start_index, end_index, damage = int(command[1]), int(command[2]), int(command[3])
  115.         if 0 <= start_index < end_index < len(ship):
  116.             for section in range(start_index, end_index + 1):
  117.                 if (ship[section] - damage) <= 0:
  118.                     print("You lost! The pirate ship has sunken.")
  119.                     exit(0)
  120.                 else:
  121.                     ship[section] -= damage
  122.  
  123.     elif command[0] == "Repair":
  124.         index, health = int(command[1]), int(command[2])
  125.         if 0 <= index < len(ship):
  126.             if ship[index] + health >= maximum_health_capacity:
  127.                 ship[index] = maximum_health_capacity
  128.             else:
  129.                 ship[index] += health
  130.  
  131.     elif command[0] == "Status":
  132.         sections_for_repair = 0
  133.         needs_repair = maximum_health_capacity * 0.2
  134.         for section in ship:
  135.             if section < needs_repair:
  136.                 sections_for_repair += 1
  137.         print(f"{sections_for_repair} sections need repair.")
  138.  
  139. print(f"Pirate ship status: {sum(ship)}")
  140. print(f"Warship status: {sum(warship)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement