Advertisement
GeorgiLukanov87

Python Advanced Exam - 27 June 2020

Sep 12th, 2022
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.58 KB | None | 0 0
  1. # Python Advanced Exam - 27 June 2020
  2. #
  3. # https://judge.softuni.org/Contests/Practice/Index/2456#0
  4.  
  5. # 01. Bombs
  6. # 02. Snake
  7. # 03. List Manipulator
  8.  
  9. -----------------------------------------------------------------------------------------------
  10.  
  11. # 01. Bombs
  12.  
  13. from collections import deque
  14.  
  15. effects = deque([int(x) for x in input().split(', ')])
  16. casings = deque([int(x) for x in input().split(', ')])
  17.  
  18. valid_crafting_result = {40: 'Datura Bombs', 60: 'Cherry Bombs', 120: 'Smoke Decoy Bombs'}
  19. crafted_bombs = {'Cherry Bombs': 0, "Datura Bombs": 0, 'Smoke Decoy Bombs': 0}
  20.  
  21. target_reached = False
  22.  
  23. while True:
  24.     if not effects or not casings:
  25.         break
  26.  
  27.     element1 = effects[0]
  28.     element2 = casings[-1]
  29.     result = element1 + element2
  30.  
  31.     if result in valid_crafting_result.keys():
  32.         crafted_bombs[valid_crafting_result[result]] += 1
  33.         effects.popleft()
  34.         casings.pop()
  35.     else:
  36.         casings[-1] -= 5
  37.  
  38.     if crafted_bombs['Cherry Bombs'] > 2 and \
  39.        crafted_bombs["Datura Bombs"] > 2 and \
  40.        crafted_bombs['Smoke Decoy Bombs'] > 2:
  41.         target_reached = True
  42.         break
  43.  
  44. if target_reached:
  45.     print(f'Bene! You have successfully filled the bomb pouch!')
  46. else:
  47.     print(f"You don't have enough materials to fill the bomb pouch.")
  48.  
  49. if effects:
  50.     print(f'Bomb Effects: {", ".join([str(el) for el in effects])}')
  51. else:
  52.     print('Bomb Effects: empty')
  53.  
  54. if casings:
  55.     print(f"Bomb Casings: {', '.join([str(el) for el in casings])}")
  56. else:
  57.     print('Bomb Casings: empty')
  58.  
  59. for bomb, count in crafted_bombs.items():
  60.     print(f'{bomb}: {count}')
  61.  
  62.  
  63. -----------------------------------------------------------------------------------------------
  64.  
  65. # 02. Snake
  66.  
  67. def is_inside_func(current_row, current_col):
  68.     return 0 <= current_row < size and 0 <= current_col < size
  69.  
  70.  
  71. def get_next_position_func(direction, row, col):
  72.     if direction == 'up':
  73.         return row - 1, col
  74.     elif direction == 'down':
  75.         return row + 1, col
  76.     elif direction == 'left':
  77.         return row, col - 1
  78.     elif direction == 'right':
  79.         return row, col + 1
  80.  
  81.  
  82. size = int(input())
  83.  
  84. burrow_holes = []
  85. snake_row = 0
  86. snake_col = 0
  87.  
  88. matrix = []  # Square size x size
  89. for row in range(size):
  90.     matrix.append(list(input()))
  91.  
  92.     for col in range(size):
  93.         if matrix[row][col] == 'S':
  94.             snake_row = row
  95.             snake_col = col
  96.  
  97.         elif matrix[row][col] == 'B':
  98.             burrow_holes.append((row, col))
  99.  
  100. food = 0
  101.  
  102. while True:
  103.     direction = input()
  104.  
  105.     next_row, next_col = get_next_position_func(direction, snake_row, snake_col)
  106.  
  107.     matrix[snake_row][snake_col] = '.'
  108.  
  109.     if is_inside_func(next_row, next_col):
  110.         if matrix[next_row][next_col] == '*':
  111.             matrix[next_row][next_col] = 'S'
  112.             food += 1
  113.             if food >= 10:
  114.                 print(f'You won! You fed the snake.')
  115.                 break
  116.         elif matrix[next_row][next_col] == 'B':
  117.             matrix[next_row][next_col] = '.'
  118.             burrow_holes.remove((next_row, next_col))
  119.             snake_row, snake_col = burrow_holes[0]
  120.             matrix[snake_row][snake_col] = 'S'
  121.             continue
  122.         else:
  123.             matrix[next_row][next_col] = 'S'
  124.  
  125.         snake_row, snake_col = next_row, next_col
  126.  
  127.     else:  # Snake is out of the matrix ! break and game over !
  128.         print('Game over!')
  129.         break
  130.  
  131. print(f'Food eaten: {food}')
  132. for el in matrix:
  133.     print(''.join(el))
  134.  
  135.  
  136. -----------------------------------------------------------------------------------------------
  137.  
  138. # 03. List Manipulator
  139.  
  140.  
  141. def list_manipulator(nums, *args):
  142.     my_list = nums.copy()
  143.  
  144.     if args[0] == 'add' and args[1] == 'beginning':
  145.         counter = 0
  146.         for el in args[2:]:
  147.             my_list.insert(counter, el)
  148.             counter += 1
  149.  
  150.     elif args[0] == 'add' and args[1] == 'end':
  151.         for el in args[2:]:
  152.             my_list.append(el)
  153.  
  154.     elif args[0] == 'remove' and args[1] == 'end':
  155.         if len(args) == 2:
  156.             my_list.pop()
  157.         else:
  158.             to_remove = args[2]
  159.             while to_remove:
  160.                 my_list.pop()
  161.                 to_remove -= 1
  162.  
  163.     elif args[0] == 'remove' and args[1] == 'beginning':
  164.         if len(args) == 2:
  165.             my_list.pop(0)
  166.         else:
  167.             to_remove = args[2]
  168.             while to_remove:
  169.                 my_list.pop(0)
  170.                 to_remove -= 1
  171.  
  172.     return my_list
  173.  
  174. -----------------------------------------------------------------------------------------------
  175.  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement