Advertisement
go6odn28

Untitled

Dec 16th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. def valid_index(lst, indx):
  2.     if 0 <= indx < len(lst):
  3.         return True
  4.     return None
  5.  
  6.  
  7. def valid_radius(targets, index, r):
  8.     if 0 <= index - r < len(targets) and index + r < len(targets):
  9.         return True
  10.     return None
  11.  
  12.  
  13. target_sequence = [int(x) for x in input().split(' ')]
  14.  
  15. while True:
  16.     command = input()
  17.     if command == 'End':
  18.         break
  19.  
  20.     info = command.split(" ")
  21.     action = info[0]
  22.  
  23.     if action == "Shoot":
  24.         shoot_idx, power = int(info[1]), int(info[2])
  25.         if valid_index(target_sequence, shoot_idx):
  26.             target_sequence[shoot_idx] -= power
  27.             if target_sequence[shoot_idx] <= 0:
  28.                 target_sequence.pop(shoot_idx)
  29.  
  30.     elif action == "Add":
  31.         add_idx, add_value = int(info[1]), int(info[2])
  32.         if valid_index(target_sequence, add_idx):
  33.             target_sequence.insert(add_idx, add_value)
  34.         else:
  35.             print("Invalid placement!")
  36.  
  37.     elif action == "Strike":
  38.         strike_idx, radius = int(info[1]), int(info[2])
  39.         if valid_radius(target_sequence, strike_idx, radius):
  40.             for i in range(radius+2):
  41.                 target_sequence.pop(strike_idx-1)
  42.  
  43.         else:
  44.             print("Strike missed!")
  45.  
  46. print('|'.join(map(str, target_sequence)))
  47.  
  48.  
  49. ################################################# with functions ###################################################
  50. def valid_index(lst, indx):
  51.     return 0 <= indx < len(lst)
  52.  
  53.  
  54. def valid_radius(targets, index, r):
  55.     return 0 <= index - r < len(targets) and index + r < len(targets)
  56.  
  57.  
  58. def shoot(targets, shoot_idx, power):
  59.     if valid_index(targets, shoot_idx):
  60.         targets[shoot_idx] -= power
  61.         if targets[shoot_idx] <= 0:
  62.             targets.pop(shoot_idx)
  63.     return targets
  64.  
  65.  
  66. def add(targets, add_idx, add_value):
  67.     if valid_index(targets, add_idx):
  68.         targets.insert(add_idx, add_value)
  69.     else:
  70.         print("Invalid placement!")
  71.     return targets
  72.  
  73.  
  74. def strike(targets, strike_idx, radius):
  75.     if valid_radius(targets, strike_idx, radius):
  76.         start = max(0, strike_idx - radius)
  77.         end = min(len(targets), strike_idx + radius + 1)
  78.         del targets[start:end]
  79.     else:
  80.         print("Strike missed!")
  81.     return targets
  82.  
  83.  
  84. def main():
  85.     target_sequence = [int(x) for x in input().split(' ')]
  86.  
  87.     while True:
  88.         command = input()
  89.         if command == 'End':
  90.             break
  91.  
  92.         info = command.split(" ")
  93.         action = info[0]
  94.  
  95.         if action == "Shoot":
  96.             shoot_idx, power = int(info[1]), int(info[2])
  97.             target_sequence = shoot(target_sequence, shoot_idx, power)
  98.  
  99.         elif action == "Add":
  100.             add_idx, add_value = int(info[1]), int(info[2])
  101.             target_sequence = add(target_sequence, add_idx, add_value)
  102.  
  103.         elif action == "Strike":
  104.             strike_idx, radius = int(info[1]), int(info[2])
  105.             target_sequence = strike(target_sequence, strike_idx, radius)
  106.  
  107.     print('|'.join(map(str, target_sequence)))
  108.  
  109.  
  110. if __name__ == "__main__":
  111.     main()
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement