Advertisement
GeorgiLukanov87

Python Advanced Exam - 14 February 2021

Sep 12th, 2022 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.20 KB | None | 0 0
  1. # Python Advanced Exam - 14 February 2021
  2. #
  3. # https://judge.softuni.org/Contests/Practice/Index/2812#2
  4.  
  5. # 01. Fireworks Show
  6. # 02. Collecting Coins
  7. # 03. Cupcake Shop
  8.  
  9. ----------------------------------------------------------------------------------------------------------
  10.  
  11. # 01. Fireworks Show
  12. # NEW VERSION
  13.  
  14. from collections import deque
  15.  
  16. effects = deque([int(x) for x in input().split(', ')])
  17. power = deque([int(x) for x in input().split(', ')])
  18.  
  19. bombs = {"Palm Fireworks": 0, "Willow Fireworks": 0, "Crossette Fireworks": 0, }
  20. success = False
  21. while effects and power:
  22.  
  23.     not_mixed = True
  24.     first_effect = effects[0]
  25.     last_power = power[-1]
  26.  
  27.     if first_effect <= 0 or last_power <= 0:
  28.         if first_effect <= 0 and effects:
  29.             effects.popleft()
  30.         if last_power <= 0 and power:
  31.             power.pop()
  32.         continue
  33.  
  34.     result = first_effect + last_power
  35.  
  36.     if result % 3 == 0 and result % 5 != 0:
  37.         bombs['Palm Fireworks'] += 1
  38.     elif result % 5 == 0 and result % 3 != 0:
  39.         bombs['Willow Fireworks'] += 1
  40.     elif result % 3 == 0 and result % 5 == 0:
  41.         bombs['Crossette Fireworks'] += 1
  42.     else:
  43.         effects.append(effects.popleft() - 1)
  44.         not_mixed = False
  45.        
  46.     if not_mixed:
  47.         effects.popleft()
  48.         power.pop()
  49.        
  50.     if bombs["Palm Fireworks"] >= 3 and bombs["Willow Fireworks"] >= 3 and bombs["Crossette Fireworks"] >= 3:
  51.         success = True
  52.         break
  53.  
  54. if success:
  55.     print("Congrats! You made the perfect firework show!")
  56. else:
  57.     print("Sorry. You can't make the perfect firework show.")
  58.  
  59. if effects:
  60.     print(f'Firework Effects left: {", ".join(str(x) for x in effects)}')
  61. if power:
  62.     print(f'Explosive Power left: {", ".join(str(x) for x in power)}')
  63.  
  64. for bomb, count in bombs.items():
  65.     print(f'{bomb}: {count}')
  66.  
  67.  
  68. ===========================================================================================================
  69.  
  70. # 01. Fireworks Show
  71. # OLD VERSION
  72.  
  73. from collections import deque
  74.  
  75. effect = deque([int(x) for x in input().split(', ') if int(x) > 0])
  76. power = deque([int(x) for x in input().split(', ') if int(x) > 0])
  77.  
  78. palm_firework = 0
  79. willow_firework = 0
  80. crossette_firework = 0
  81.  
  82. success = False
  83. while True:
  84.     if not effect or not power:
  85.         break
  86.  
  87.     first_effect = effect[0]
  88.     last_power = power[-1]
  89.  
  90.     result = first_effect + last_power
  91.     if result % 3 == 0 and result % 5 != 0:
  92.         palm_firework += 1
  93.         effect.popleft()
  94.         power.pop()
  95.  
  96.     elif result % 5 == 0 and result % 3 != 0:
  97.         willow_firework += 1
  98.         effect.popleft()
  99.         power.pop()
  100.  
  101.     elif result % 3 == 0 and result % 5 == 0:
  102.         crossette_firework += 1
  103.         effect.popleft()
  104.         power.pop()
  105.  
  106.     else:
  107.         removed_effect = effect.popleft()
  108.         removed_effect -= 1
  109.         if removed_effect > 0:
  110.             effect.append(removed_effect)
  111.  
  112.     if palm_firework >= 3 and willow_firework >= 3 and crossette_firework >= 3:
  113.         success = True
  114.         break
  115.  
  116. if success:
  117.     print('Congrats! You made the perfect firework show!')
  118. else:
  119.     print("Sorry. You can't make the perfect firework show.")
  120.  
  121. if effect:
  122.     print(f'Firework Effects left: {", ".join(str(e) for e in effect)}')
  123.  
  124. if power:
  125.     print(f'Explosive Power left: {", ".join(str(p) for p in power)}')
  126.  
  127. print(f'Palm Fireworks: {palm_firework}')
  128. print(f'Willow Fireworks: {willow_firework}')
  129. print(f'Crossette Fireworks: {crossette_firework}')
  130.  
  131.  
  132. ----------------------------------------------------------------------------------------------------------
  133.  
  134. # 02. Collecting Coins
  135. # NEW VERSION
  136.  
  137. def validate_step(r, c):
  138.     if r >= SIZE:
  139.         r = 0
  140.     elif r < 0:
  141.         r = (SIZE - 1)
  142.     if c >= SIZE:
  143.         c = 0
  144.     elif c < 0:
  145.         c = (SIZE - 1)
  146.     return r, c
  147.  
  148.  
  149. def get_next_position(direction, r, c):
  150.     if direction == 'left':
  151.         return validate_step(r, c - 1)
  152.     elif direction == 'right':
  153.         return validate_step(r, c + 1)
  154.     elif direction == 'up':
  155.         return validate_step(r - 1, c)
  156.     elif direction == 'down':
  157.         return validate_step(r + 1, c)
  158.  
  159.  
  160. SIZE = int(input())
  161. matrix = []
  162. row, col = 0, 0
  163.  
  164. for row_index in range(SIZE):
  165.     matrix.append(input().split())
  166.     for col_index in range(SIZE):
  167.         if matrix[row_index][col_index] == 'P':
  168.             row, col = row_index, col_index
  169.  
  170. points = []
  171. path = [[row, col]]
  172. while True:
  173.     command = input()
  174.     if command == "":
  175.         break
  176.     if command not in ['up', 'down', 'left', 'right']:
  177.         continue
  178.  
  179.     row, col = get_next_position(command, row, col)
  180.     current_step = matrix[row][col]
  181.  
  182.     path.append([row, col])
  183.  
  184.     if current_step == 'X':
  185.         print(f"Game over! You've collected {sum(points) // 2} coins.")
  186.         break
  187.  
  188.     if not current_step.isalpha():
  189.         points.append(int(current_step))
  190.         matrix[row][col] = '0'
  191.  
  192.     if sum(points) >= 100:
  193.         print(f"You won! You've collected {sum(points)} coins.")
  194.         break
  195.  
  196. print('Your path:')
  197. for step in path:
  198.     print(step)
  199.  
  200.  
  201. =============================================================================================================
  202.  
  203. # 02. Collecting Coins
  204. # OLD VERSION
  205.  
  206. import math
  207.  
  208.  
  209. def is_inside(some_row, some_col):  # validating if cell is inside the matrix.
  210.     return 0 <= some_row < size and 0 <= some_col < size
  211.  
  212.  
  213. def get_next_position_func(direction, row, col):
  214.     if direction == 'up':
  215.         return row - 1, col
  216.     elif direction == 'down':
  217.         return row + 1, col
  218.     elif direction == 'left':
  219.         return row, col - 1
  220.     elif direction == 'right':
  221.         return row, col + 1
  222.  
  223.  
  224. size = int(input())
  225. p_row = 0
  226. p_col = 0
  227. matrix = []
  228. for row_index in range(size):
  229.     matrix.append(input().split())
  230.     for col_index in range(size):
  231.         if matrix[row_index][col_index] == 'P':
  232.             p_row = row_index
  233.             p_col = col_index
  234.  
  235.  
  236. wall_hit = False
  237. coins = 0
  238. path = [[p_row, p_col]]
  239.  
  240. while True:
  241.     command = input()
  242.     matrix[p_row][p_col] = '0'
  243.     next_row, next_col = get_next_position_func(command, p_row, p_col)
  244.     path.append([next_row, next_col])
  245.     if is_inside(next_row, next_col):
  246.  
  247.         if matrix[next_row][next_col] != 'X':
  248.             coins += int(matrix[next_row][next_col])
  249.             matrix[next_row][next_col] = '0'
  250.             p_row, p_col = next_row, next_col
  251.  
  252.         else:  # STEP ON X , GAME OVER
  253.             wall_hit = True
  254.             break
  255.  
  256.     else:  # OUTSIDE THE FIELD !
  257.         if command == 'down':
  258.             p_row = next_row - size
  259.         elif command == 'left':
  260.             p_col = next_col + size
  261.         elif command == 'up':
  262.             p_row = next_row + size
  263.         elif command == 'right':
  264.             p_col = next_col - size
  265.         path.pop(-1)
  266.         path.append([p_row, p_col])
  267.         if str(matrix[p_row][p_col]).isdigit() and matrix[p_row][p_col] != 'X':
  268.             coins += int(matrix[p_row][p_col])
  269.             matrix[p_row][p_col] = '0'
  270.         else:
  271.             wall_hit = True
  272.             break
  273.  
  274.     if coins >= 100 or wall_hit:
  275.         break
  276.  
  277. if coins >= 100:
  278.     print(f"You won! You've collected {coins} coins.")
  279. else:
  280.     if wall_hit:
  281.         coins /= 2
  282.         print(f"Game over! You've collected {math.floor(coins)} coins.")
  283.  
  284. print('Your path:')
  285. print(*path, sep='\n')
  286.  
  287.  
  288. ----------------------------------------------------------------------------------------------------------
  289.  
  290. # 03. Cupcake Shop
  291.  
  292.  
  293. def stock_availability(my_list, action, *args):
  294.     sell_products = []
  295.     if action == 'delivery':
  296.         for el in args:
  297.             my_list.append(el)
  298.  
  299.     elif action == 'sell':
  300.         if not args:
  301.             my_list.pop(0)
  302.  
  303.         for el in args:
  304.             sell_products.append(el)
  305.  
  306.         for el in sell_products:
  307.             if str(el).isdigit():
  308.                 while el:
  309.                     my_list.pop(0)
  310.                     el -= 1
  311.             else:
  312.                 while el in my_list:
  313.                     my_list.remove(el)
  314.  
  315.     return my_list
  316.  
  317. ----------------------------------------------------------------------------------------------------------
  318.  
  319.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement