Advertisement
SimeonTs

SUPyF2 P.-Mid-Exam/2 November 2019/2. - Tank Collector

Nov 5th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. """
  2. Programming Fundamentals Mid Exam - 2 November 2019 Group 2
  3. Check your code: https://judge.softuni.bg/Contests/1862/Programming-Fundamentals-Mid-Exam-2-November-2019-Group-2
  4.  
  5. SUPyF2 P.-Mid-Exam/2 November 2019/2. - Tank Collector
  6.  
  7. Problem:
  8. Tom is a world of tanks player and he likes to collect premium tanks.
  9. You will receive a list of Tom's already owned premium vehicles on a single line separated by ", ".
  10. On the next n lines you will receive commands that could be:
  11. • Add, {tankName}: Check if he already owns the wanted tank.
  12. • If he owns it, print on console:  "Tank is already bought"
  13. • If not, print on console:  "Tank successfully bought" and add it to the tank list.
  14. • Remove, {tankName}: Check if he owns the tank.
  15. • If he owns it print on console:  "Tank successfully sold" and remove it from the tank list.
  16. • If not print on console: "Tank not found"
  17. • Remove At, {index}: Check if the index is in the range of the list.
  18. • If not print on console: "Index out of range" and continue.
  19. • If it’s in range, remove at the given index and print on console: "Tank successfully sold"
  20. • Insert, {index}, {tankName}: Check if the index is in range of the list and check if he already owns the tank.
  21. • If not print on console: "Index out of range" and continue.
  22. • If it's in range and he doesn't own the tank then add the tank at the given index and print on console:
  23. • "Tank successfully bought"
  24. • If the tank is in range and he owns it already than print on console:
  25. • "Tank is already bought"
  26. After you go through all the commands you need to print the list on a single line separated by ", ".
  27.  
  28. Input:
  29. • The first input line will contain the list of already owned tanks.
  30. • The second input line  will be  the number of commands – an integer number in range [0…50].
  31. • On the next input lines you will be receiving commands.
  32.  
  33. Output:
  34. • As output you must print a single line containing the elements of the list, joined by  ", ".
  35.  
  36. Examples:
  37.  
  38. Input:
  39. T-34-85 Rudy, SU-100Y, STG
  40. 3
  41. Add, King Tiger(C)
  42. Insert, 2, IS-2M
  43. Remove, T-34-85 Rudy
  44.  
  45. Output:
  46. Tank successfully bought
  47. Tank successfully bought
  48. Tank successfully sold
  49. SU-100Y, IS-2M, STG, King Tiger(C)
  50.  
  51. Comments:
  52. The first command gives the tank list so its splitted and added into the list.
  53. "T-34-85 Rudy, SU-100Y, STG"
  54. The second commands gives the number of commands that will be received.
  55. "3"
  56. The Add command adds the tank to the list after the necessary checks.
  57. "Add, King Tiger(C)" – "Tank successfully bought"
  58. The Insert commands also adds the tank at the given spot after the necessary checks.
  59. "Insert, 2, IS-2M" – "Tank successfully bought"
  60. The Remove command is the last one and after the checks the tank is sold.
  61. "Remove, T-34-85 Rudy" – "Tank successfully sold"
  62. After that we print the list on the console.
  63. "SU-100Y, IS-2M, STG, King Tiger(C)"
  64.  
  65. Input:
  66. T 34, T 34 B, T92, AMX 13 57
  67. 4
  68. Add, T 34
  69. Remove, AMX CDC
  70. Insert, 10, M60
  71. Remove At, 1
  72.  
  73. Output:
  74. Tank is already bought
  75. Tank not found
  76. Index out of range
  77. Tank successfully sold
  78. T 34, T92, AMX 13 57
  79. """
  80. owned_tanks = input().split(", ")
  81. num = int(input())
  82.  
  83. for i in range(num):
  84.     command = input().split(", ")
  85.  
  86.     if command[0] == "Add":
  87.         tank_name = command[1]
  88.         if tank_name in owned_tanks:
  89.             print(f"Tank is already bought")
  90.         else:
  91.             owned_tanks.append(tank_name)
  92.             print("Tank successfully bought")
  93.  
  94.     elif command[0] == "Remove":
  95.         tank_name = command[1]
  96.         if tank_name in owned_tanks:
  97.             owned_tanks.remove(tank_name)
  98.             print("Tank successfully sold")
  99.         else:
  100.             print("Tank not found")
  101.  
  102.     elif command[0] == "Remove At":
  103.         index = int(command[1])
  104.         if 0 <= index < len(owned_tanks):
  105.             owned_tanks.pop(index)
  106.             print(f"Tank successfully sold")
  107.         else:
  108.             print(f"Index out of range")
  109.  
  110.     elif command[0] == "Insert":
  111.         index, tank_name = int(command[1]), command[2]
  112.         if 0 <= index < len(owned_tanks):
  113.             if tank_name not in owned_tanks:
  114.                 owned_tanks.insert(index, tank_name)
  115.                 print(f"Tank successfully bought")
  116.             else:
  117.                 print("Tank is already bought")
  118.         else:
  119.             print(f"Index out of range")
  120.  
  121. print(', '.join(owned_tanks))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement