Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Stacks, Queues, Tuples and Sets - Exercise
- # https://judge.softuni.org/Contests/Compete/Index/3159#4
- --------------------------------------------------------------------------------------------------------
- # 01. Numbers
- # 02. Expression Evaluator
- # 03. Milkshakes
- # 04. Honey
- # 05. Santa's Present Factory
- # 06. Paint Colors
- -------------------------------------------------------------------------------------------------------
- # 01. Numbers
- from collections import deque
- num1 = {int(x) for x in input().split()}
- num2 = {int(x) for x in input().split()}
- n = int(input())
- for _ in range(n):
- command = input().split()
- if command[0] == 'Add':
- if command[1] == 'First':
- nums_to_add = deque([int(x) for x in command[2:]])
- while nums_to_add:
- num1.add(nums_to_add.popleft())
- elif command[1] == 'Second':
- nums_to_add = deque([int(x) for x in command[2:]])
- while nums_to_add:
- num2.add(nums_to_add.popleft())
- elif command[0] == 'Remove':
- if command[1] == 'First':
- nums_to_remove = [int(x) for x in command[2:]]
- while nums_to_remove:
- remove_el = nums_to_remove.pop()
- if remove_el in num1:
- num1.remove(remove_el)
- elif command[1] == 'Second':
- nums_to_remove = [int(x) for x in command[2:]]
- while nums_to_remove:
- remove_el = nums_to_remove.pop()
- if remove_el in num2:
- num2.remove(remove_el)
- elif command[0] == 'Check':
- if num2.issubset(num1) or num1.issubset(num2):
- print('True')
- else:
- print('False')
- num1 = sorted(num1)
- num2 = sorted(num2)
- print(*num1, sep=', ')
- print(*num2, sep=', ')
- --------------------------------------------------------------------------------------------------------
- # 02. Expression Evaluator
- from math import floor
- import functools
- data = input().split()
- final_result = []
- nums = []
- operators = []
- expression = []
- for el in data:
- if el in ["*", "+", "-", "/"]:
- result = 0
- if el == '+':
- result = functools.reduce(lambda x, y: x + y, nums)
- elif el == '-':
- result = functools.reduce(lambda x, y: x - y, nums)
- elif el == '*':
- result = functools.reduce(lambda x, y: x * y, nums)
- elif el == '/':
- result = functools.reduce(lambda x, y: x / y, nums)
- final_result.append(floor(result))
- nums.clear()
- nums.append(floor(result))
- else:
- nums.append(int(el))
- print(final_result[-1])
- --------------------------------------------------------------------------------------------------------
- # 03. Milkshakes
- from collections import deque
- choco = deque([int(x) for x in input().split(', ')])
- cups_milk = deque([int(x) for x in input().split(', ')])
- shake_ready = 0
- while True:
- if not choco or not cups_milk:
- break
- last_choco = choco[-1]
- first_cup_milk = cups_milk[0]
- if last_choco <= 0 or first_cup_milk <= 0:
- if choco and last_choco <= 0:
- choco.pop()
- if cups_milk and first_cup_milk <= 0:
- cups_milk.popleft()
- continue
- if last_choco == first_cup_milk:
- choco.pop()
- cups_milk.popleft()
- shake_ready += 1
- else:
- cups_milk.popleft()
- cups_milk.append(first_cup_milk)
- choco[-1] -= 5
- if shake_ready == 5:
- break
- if shake_ready >= 5:
- print("Great! You made all the chocolate milkshakes needed!")
- else:
- print("Not enough milkshakes.")
- if choco:
- print(f'Chocolate: {", ".join(str(x) for x in choco)}')
- else:
- print("Chocolate: empty")
- if cups_milk:
- print(f'Milk: {", ".join(str(x) for x in cups_milk)}')
- else:
- print("Milk: empty")
- --------------------------------------------------------------------------------------------------------
- # 04. Honey
- from collections import deque
- import functools
- def operate(opers, args):
- result = 0
- if args[1] == 0:
- return 0
- if opers == '+':
- result = functools.reduce(lambda x, y: x + y, args)
- elif opers == '-':
- result = functools.reduce(lambda x, y: x - y, args)
- elif opers == '*':
- result = functools.reduce(lambda x, y: x * y, args)
- elif opers == '/':
- result = functools.reduce(lambda x, y: x / y, args)
- return abs(result)
- total_honey_made = 0
- bees = deque([int(x) for x in input().split()])
- nectar = deque([int(x) for x in input().split()])
- process = deque(input().split())
- while True:
- if not bees or not nectar:
- break
- first_bee = bees[0]
- last_nectar = nectar[-1]
- if last_nectar >= first_bee:
- matches = [bees.popleft(), nectar.pop()]
- total_honey_made += operate(process.popleft(), matches)
- else:
- nectar.pop()
- print(f'Total honey made: {total_honey_made}')
- if bees:
- print(f'Bees left: {", ".join(str(x) for x in bees)}')
- if nectar:
- print(f'Nectar left: {", ".join(str(x) for x in nectar)}')
- --------------------------------------------------------------------------------------------------------
- # 05. Santa's Present Factory
- from collections import deque
- materials = deque([int(x) for x in input().split()])
- magic = deque([int(x) for x in input().split()])
- price_list = {150: 'Doll', 250: 'Wooden train', 300: 'Teddy bear', 400: 'Bicycle'}
- crafted_gifts = {'Doll': 0, 'Wooden train': 0, 'Teddy bear': 0, 'Bicycle': 0}
- while materials and magic:
- last_material = materials[-1]
- first_magic = magic[0]
- result = last_material * first_magic
- if last_material == 0 or first_magic == 0:
- if materials:
- if last_material == 0:
- materials.pop()
- if magic:
- if first_magic == 0:
- magic.popleft()
- continue
- if result in price_list:
- crafted_gifts[price_list[result]] += 1
- materials.pop()
- magic.popleft()
- else:
- if result < 0:
- materials.append(materials.pop() + magic.popleft())
- elif result > 0:
- magic.popleft()
- materials[-1] += 15
- if (crafted_gifts['Doll'] and crafted_gifts['Wooden train'])\
- or (crafted_gifts['Teddy bear'] and crafted_gifts['Bicycle']):
- print("The presents are crafted! Merry Christmas!")
- else:
- print("No presents this Christmas!")
- if materials:
- print(f"Materials left: {', '.join(str(x) for x in reversed(materials))}")
- if magic:
- print(f"Magic left: {', '.join(str(x) for x in magic)}")
- for toy, count in sorted(crafted_gifts.items(), key=lambda x: x[0]):
- if count:
- print(f'{toy}: {count}')
- --------------------------------------------------------------------------------------------------------
- # 06. Paint Colors
- from collections import deque
- data = deque(input().split())
- colors_found = []
- all_colors = ['red', 'yellow', 'blue', 'orange', 'purple', 'green']
- while data:
- if len(data) > 1:
- first = data.popleft()
- second = data.pop()
- substring1 = first + second
- substring2 = second + first
- if substring1 in all_colors:
- colors_found.append(substring1)
- elif substring2 in all_colors:
- colors_found.append(substring2)
- else:
- first_el = first[:-1]
- second_el = second[:-1]
- if first_el:
- data.insert(len(data) // 2, first_el)
- if second_el:
- data.insert(len(data) // 2, second_el)
- elif len(data) <= 1:
- result = data.popleft()
- if result in all_colors:
- colors_found.append(result)
- final_print = []
- for color in colors_found:
- if color not in all_colors[3:]:
- final_print.append(color)
- else:
- if color == 'orange':
- if 'red' in colors_found and 'yellow' in colors_found:
- final_print.append(color)
- elif color == 'purple':
- if 'red' in colors_found and 'blue' in colors_found:
- final_print.append(color)
- elif color == 'green':
- if 'yellow' in colors_found and 'blue' in colors_found:
- final_print.append(color)
- print(final_print)
- --------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement