Advertisement
GeorgiLukanov87

Stacks, Queues, Tuples and Sets - Exercise

Sep 16th, 2022 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.50 KB | None | 0 0
  1. # Stacks, Queues, Tuples and Sets - Exercise
  2.  
  3. # https://judge.softuni.org/Contests/Compete/Index/3159#4
  4. --------------------------------------------------------------------------------------------------------
  5.  
  6. # 01. Numbers
  7. # 02. Expression Evaluator
  8. # 03. Milkshakes
  9. # 04. Honey
  10. # 05. Santa's Present Factory
  11. # 06. Paint Colors
  12.  
  13. -------------------------------------------------------------------------------------------------------
  14.  
  15. # 01. Numbers
  16.  
  17.  
  18. from collections import deque
  19.  
  20. num1 = {int(x) for x in input().split()}
  21. num2 = {int(x) for x in input().split()}
  22.  
  23. n = int(input())
  24. for _ in range(n):
  25.     command = input().split()
  26.     if command[0] == 'Add':
  27.         if command[1] == 'First':
  28.             nums_to_add = deque([int(x) for x in command[2:]])
  29.             while nums_to_add:
  30.                 num1.add(nums_to_add.popleft())
  31.  
  32.         elif command[1] == 'Second':
  33.             nums_to_add = deque([int(x) for x in command[2:]])
  34.             while nums_to_add:
  35.                 num2.add(nums_to_add.popleft())
  36.  
  37.     elif command[0] == 'Remove':
  38.         if command[1] == 'First':
  39.             nums_to_remove = [int(x) for x in command[2:]]
  40.             while nums_to_remove:
  41.                 remove_el = nums_to_remove.pop()
  42.                 if remove_el in num1:
  43.                     num1.remove(remove_el)
  44.  
  45.         elif command[1] == 'Second':
  46.             nums_to_remove = [int(x) for x in command[2:]]
  47.             while nums_to_remove:
  48.                 remove_el = nums_to_remove.pop()
  49.                 if remove_el in num2:
  50.                     num2.remove(remove_el)
  51.  
  52.     elif command[0] == 'Check':
  53.         if num2.issubset(num1) or num1.issubset(num2):
  54.             print('True')
  55.         else:
  56.             print('False')
  57.  
  58. num1 = sorted(num1)
  59. num2 = sorted(num2)
  60. print(*num1, sep=', ')
  61. print(*num2, sep=', ')
  62.  
  63.  
  64. --------------------------------------------------------------------------------------------------------
  65.  
  66. # 02. Expression Evaluator
  67.  
  68.  
  69. from math import floor
  70. import functools
  71.  
  72. data = input().split()
  73. final_result = []
  74. nums = []
  75. operators = []
  76. expression = []
  77. for el in data:
  78.     if el in ["*", "+", "-", "/"]:
  79.         result = 0
  80.         if el == '+':
  81.             result = functools.reduce(lambda x, y: x + y, nums)
  82.         elif el == '-':
  83.             result = functools.reduce(lambda x, y: x - y, nums)
  84.         elif el == '*':
  85.             result = functools.reduce(lambda x, y: x * y, nums)
  86.         elif el == '/':
  87.             result = functools.reduce(lambda x, y: x / y, nums)
  88.  
  89.         final_result.append(floor(result))
  90.         nums.clear()
  91.         nums.append(floor(result))
  92.  
  93.     else:
  94.         nums.append(int(el))
  95.  
  96. print(final_result[-1])
  97.  
  98.  
  99. --------------------------------------------------------------------------------------------------------
  100.  
  101. # 03. Milkshakes
  102.  
  103.  
  104. from collections import deque
  105.  
  106. choco = deque([int(x) for x in input().split(', ')])
  107. cups_milk = deque([int(x) for x in input().split(', ')])
  108. shake_ready = 0
  109.  
  110. while True:
  111.     if not choco or not cups_milk:
  112.         break
  113.  
  114.     last_choco = choco[-1]
  115.     first_cup_milk = cups_milk[0]
  116.  
  117.     if last_choco <= 0 or first_cup_milk <= 0:
  118.         if choco and last_choco <= 0:
  119.             choco.pop()
  120.         if cups_milk and first_cup_milk <= 0:
  121.             cups_milk.popleft()
  122.         continue
  123.  
  124.     if last_choco == first_cup_milk:
  125.         choco.pop()
  126.         cups_milk.popleft()
  127.         shake_ready += 1
  128.     else:
  129.         cups_milk.popleft()
  130.         cups_milk.append(first_cup_milk)
  131.         choco[-1] -= 5
  132.  
  133.     if shake_ready == 5:
  134.         break
  135.  
  136. if shake_ready >= 5:
  137.     print("Great! You made all the chocolate milkshakes needed!")
  138. else:
  139.     print("Not enough milkshakes.")
  140.  
  141. if choco:
  142.     print(f'Chocolate: {", ".join(str(x) for x in choco)}')
  143. else:
  144.     print("Chocolate: empty")
  145.  
  146. if cups_milk:
  147.     print(f'Milk: {", ".join(str(x) for x in cups_milk)}')
  148. else:
  149.     print("Milk: empty")
  150.  
  151.    
  152. --------------------------------------------------------------------------------------------------------
  153.  
  154. # 04. Honey
  155.  
  156.  
  157. from collections import deque
  158. import functools
  159.  
  160.  
  161. def operate(opers, args):
  162.     result = 0
  163.     if args[1] == 0:
  164.         return 0
  165.     if opers == '+':
  166.         result = functools.reduce(lambda x, y: x + y, args)
  167.     elif opers == '-':
  168.         result = functools.reduce(lambda x, y: x - y, args)
  169.     elif opers == '*':
  170.         result = functools.reduce(lambda x, y: x * y, args)
  171.     elif opers == '/':
  172.         result = functools.reduce(lambda x, y: x / y, args)
  173.  
  174.     return abs(result)
  175.  
  176.  
  177. total_honey_made = 0
  178. bees = deque([int(x) for x in input().split()])
  179. nectar = deque([int(x) for x in input().split()])
  180. process = deque(input().split())
  181.  
  182. while True:
  183.     if not bees or not nectar:
  184.         break
  185.     first_bee = bees[0]
  186.     last_nectar = nectar[-1]
  187.  
  188.     if last_nectar >= first_bee:
  189.         matches = [bees.popleft(), nectar.pop()]
  190.         total_honey_made += operate(process.popleft(), matches)
  191.     else:
  192.         nectar.pop()
  193.  
  194. print(f'Total honey made: {total_honey_made}')
  195. if bees:
  196.     print(f'Bees left: {", ".join(str(x) for x in bees)}')
  197.  
  198. if nectar:
  199.     print(f'Nectar left: {", ".join(str(x) for x in nectar)}')
  200.  
  201.  
  202. --------------------------------------------------------------------------------------------------------
  203.  
  204. # 05. Santa's Present Factory
  205.  
  206.  
  207. from collections import deque
  208.  
  209. materials = deque([int(x) for x in input().split()])
  210. magic = deque([int(x) for x in input().split()])
  211.  
  212. price_list = {150: 'Doll', 250: 'Wooden train', 300: 'Teddy bear', 400: 'Bicycle'}
  213. crafted_gifts = {'Doll': 0, 'Wooden train': 0, 'Teddy bear': 0, 'Bicycle': 0}
  214.  
  215. while materials and magic:
  216.     last_material = materials[-1]
  217.     first_magic = magic[0]
  218.     result = last_material * first_magic
  219.  
  220.     if last_material == 0 or first_magic == 0:
  221.         if materials:
  222.             if last_material == 0:
  223.                 materials.pop()
  224.         if magic:
  225.             if first_magic == 0:
  226.                 magic.popleft()
  227.         continue
  228.  
  229.     if result in price_list:
  230.         crafted_gifts[price_list[result]] += 1
  231.         materials.pop()
  232.         magic.popleft()
  233.     else:
  234.         if result < 0:
  235.             materials.append(materials.pop() + magic.popleft())
  236.         elif result > 0:
  237.             magic.popleft()
  238.             materials[-1] += 15
  239.  
  240. if (crafted_gifts['Doll'] and crafted_gifts['Wooden train'])\
  241.         or (crafted_gifts['Teddy bear'] and crafted_gifts['Bicycle']):
  242.     print("The presents are crafted! Merry Christmas!")
  243. else:
  244.     print("No presents this Christmas!")
  245.  
  246. if materials:
  247.     print(f"Materials left: {', '.join(str(x) for x in reversed(materials))}")
  248. if magic:
  249.     print(f"Magic left: {', '.join(str(x) for x in magic)}")
  250.  
  251. for toy, count in sorted(crafted_gifts.items(), key=lambda x: x[0]):
  252.     if count:
  253.         print(f'{toy}: {count}')
  254.  
  255.        
  256. --------------------------------------------------------------------------------------------------------
  257.  
  258. # 06. Paint Colors
  259.  
  260.  
  261. from collections import deque
  262.  
  263. data = deque(input().split())
  264. colors_found = []
  265. all_colors = ['red', 'yellow', 'blue', 'orange', 'purple', 'green']
  266.  
  267. while data:
  268.     if len(data) > 1:
  269.         first = data.popleft()
  270.         second = data.pop()
  271.         substring1 = first + second
  272.         substring2 = second + first
  273.         if substring1 in all_colors:
  274.             colors_found.append(substring1)
  275.         elif substring2 in all_colors:
  276.             colors_found.append(substring2)
  277.         else:
  278.             first_el = first[:-1]
  279.             second_el = second[:-1]
  280.             if first_el:
  281.                 data.insert(len(data) // 2, first_el)
  282.             if second_el:
  283.                 data.insert(len(data) // 2, second_el)
  284.  
  285.     elif len(data) <= 1:
  286.         result = data.popleft()
  287.         if result in all_colors:
  288.             colors_found.append(result)
  289.  
  290. final_print = []
  291. for color in colors_found:
  292.     if color not in all_colors[3:]:
  293.         final_print.append(color)
  294.     else:
  295.         if color == 'orange':
  296.             if 'red' in colors_found and 'yellow' in colors_found:
  297.                 final_print.append(color)
  298.         elif color == 'purple':
  299.             if 'red' in colors_found and 'blue' in colors_found:
  300.                 final_print.append(color)
  301.         elif color == 'green':
  302.             if 'yellow' in colors_found and 'blue' in colors_found:
  303.                 final_print.append(color)
  304.  
  305. print(final_print)
  306.  
  307.  
  308.  
  309. --------------------------------------------------------------------------------------------------------
  310.  
  311.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement