Advertisement
ALEXANDAR_GEORGIEV

Set and Tuple 2

May 15th, 2023
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.14 KB | Source Code | 0 0
  1. # Expression_eveluator
  2. from collections import deque
  3. from math import floor
  4.  
  5. expression = deque(input().split())
  6.  
  7. idx = 0
  8.  
  9. while idx < len(expression):
  10.     element = expression[idx]
  11.  
  12. #    eval()      # Прави стринга в числа
  13.  
  14.     if element == "*":
  15.         for _ in range(idx - 1):
  16.             expression.appendleft(int(expression.popleft()) * int(expression.popleft()))
  17.     elif element == "/":
  18.         for _ in range(idx - 1):
  19.             expression.appendleft(int(expression.popleft()) / int(expression.popleft()))
  20.     elif element == "+":
  21.         for _ in range(idx - 1):
  22.             expression.appendleft(int(expression.popleft()) + int(expression.popleft()))
  23.     elif element == "-":
  24.         for _ in range(idx - 1):
  25.             expression.appendleft(int(expression.popleft()) - int(expression.popleft()))
  26.  
  27.     if element in "*/+-":
  28.         del expression[1]
  29.         idx = 1
  30.  
  31.  
  32.     idx += 1
  33.  
  34. print(floor(int(expression[0])))
  35.  
  36. # Solution 2
  37. from math import floor
  38. from functools import reduce
  39.  
  40. expression = input().split()
  41.  
  42. idx = 0
  43.  
  44. functions = {
  45.     "*": lambda i: reduce(lambda a, b: a * b, map(int, expression[:i])),
  46.     "/": lambda i: reduce(lambda a, b: a / b, map(int, expression[:i])),
  47.     "+": lambda i: reduce(lambda a, b: a + b, map(int, expression[:i])),
  48.     "-": lambda i: reduce(lambda a, b: a - b, map(int, expression[:i]))
  49. }
  50.  
  51. while idx < len(expression):
  52.     element = expression[idx]
  53.  
  54.     if element in "*/+-":
  55.         expression[0] = functions[element](idx)
  56.         [expression.pop(1) for i in range(idx)]
  57.         idx = 1
  58.  
  59.     idx += 1
  60.  
  61. print(floor(int(expression[0])))
  62.  
  63.  
  64. # Honey
  65. from collections import deque
  66.  
  67. bees = deque(int(x) for x in input().split())
  68. nectar = deque(int(x) for x in input().split())
  69. symbols = deque(input().split())
  70.  
  71. total_honey = 0
  72.  
  73. operations = {
  74.     "*": lambda x, y: x * y,
  75.     "/": lambda x, y: x / y,
  76.     "+": lambda x, y: x + y,
  77.     "-": lambda x, y: x - y,
  78. }
  79.  
  80. while bees and nectar:
  81.     curr_bee = bees.popleft()
  82.  
  83.     curr_nectar = nectar.pop()
  84.  
  85.     if curr_nectar < curr_bee:
  86.         bees.appendleft(curr_bee)
  87.  
  88.     elif curr_nectar > curr_bee:
  89.         total_honey += abs(operations[symbols.popleft()](curr_bee, curr_nectar))
  90.  
  91. print(f"Total honey made: {total_honey}")
  92.  
  93. if bees:
  94.     print(f"Bees left: {', '.join(str(x) for x in bees)}")
  95.  
  96. if nectar:
  97.     print(f"Nectar left: {', '.join(str(x) for x in nectar)}")
  98.  
  99.  
  100. # Milkshakes
  101. from collections import deque
  102.  
  103. chocolates = deque(int(x) for x in input().split(", "))
  104. cups_of_milk = deque(int(x) for x in input().split(", "))
  105.  
  106. milkshakes = 0
  107.  
  108. while milkshakes != 5 and chocolates and cups_of_milk:
  109.     chocolate = chocolates.pop()
  110.     cup_of_milk = cups_of_milk.pop()
  111.  
  112.     if chocolate <= 0 and cup_of_milk <= 0:
  113.         continue
  114.     elif chocolate <= 0:
  115.         cups_of_milk.appendleft(cup_of_milk)
  116.         continue
  117.     elif cup_of_milk <= 0:
  118.         chocolates.append(chocolate)
  119.         continue
  120.  
  121.     if chocolate == cup_of_milk:
  122.         milkshakes += 1
  123.     else:
  124.         cups_of_milk.append(cup_of_milk)
  125.         chocolates.append(chocolate - 5)
  126.  
  127. if milkshakes == 5:
  128.     print("Greates!")
  129. else:
  130.     print("Not enough milkshakes")
  131.  
  132. print(f"Chokolate: {', '.join(str(x) for x in chocolates) or 'empty' }")
  133. print(f"Milk: {', '.join(str(x) for x in cups_of_milk) or 'empty' }")
  134.  
  135.  
  136. # Numbers
  137. first = set(int(x) for x in input().split())
  138. second = set(int(x) for x in input().split())
  139.  
  140. for _ in range(int(input())):
  141.     first_command, second_command, *data = input().split() # Вземи всичко след първите 2 елемента и го раопакояй отделно за всичко
  142.  
  143.     command = first_command + " " + second_command
  144.  
  145.     if command == " Add First":
  146.         [first.add(int(el)) for el in data]
  147.     elif command == "Add Second":
  148.         [second.add(int(el)) for el in data]
  149.     elif command == "Remove First":
  150.         [first.discard(int(el)) for el in data]
  151.     elif command == "Remove Second":
  152.         [second.discard(int(el)) for el in data]
  153.     else:
  154.         print(first.issubset(second) or second.issubset(first)) # Връща True or False
  155.  
  156.  
  157. print(*sorted(first), sep=", ")
  158. print(*sorted(second), sep=", ")
  159.  
  160. # Вместо IF -> Solution 2
  161. first = set(int(x) for x in input().split())
  162. second = set(int(x) for x in input().split())
  163.  
  164. function = {
  165.     "Add First": lambda x: [first.add(el) for el in data],
  166.     "Add Second": lambda x: [second.add(el) for el in data],
  167.     "Remove First": lambda x: [first.discard(el) for el in data],
  168.     "Remove Second": lambda x: [second.discard(el) for el in data],
  169.     "Check Subset": lambda x: print(first.issubset(second) or second.issubset(first)) # Връща True or False
  170. }
  171.  
  172. for _ in range(int(input())):
  173.     first_command, second_command, *data = input().split() # Вземи всичко след първите 2 елемента и го раопакояй отделно за всичко
  174.  
  175.     command = first_command + " " + second_command
  176.  
  177.     function[command](int(x) for x in data)
  178.  
  179.  
  180. print(*sorted(first), sep=", ")
  181. print(*sorted(second), sep=", ")
  182.  
  183.  
  184.  
  185. # Paint_colors
  186. from collections import deque
  187.  
  188. words = deque(input().split())
  189.  
  190. colors = {"red", "yellow", "blue", "orange", "purple", "green"}
  191. req_colors = {
  192.     "orange": {"yellow", "red"},
  193.     "purple": {"red", "blue"},
  194.     "green": {"yellow", "blue"}
  195. }
  196.  
  197. result = []
  198.  
  199. while words:
  200.     first_word = words.popleft()
  201.     second_word = words.pop() if words else ""
  202.  
  203.     for color in (first_word + second_word, second_word + first_word):
  204.         if color in colors:
  205.             result.append(color)
  206.             break
  207.  
  208.     else:
  209.         for el in (first_word[:-1], second_word[:-1]):
  210.             if el:
  211.                 words.insert(len(words) // 2, el)
  212.  
  213.  
  214. for color in set(req_colors.keys()).intersection(result):
  215.     if not req_colors[color].issubset(result):
  216.         result.remove(color)
  217.  
  218.  
  219.  
  220.  
  221. print(result)
  222.  
  223.  
  224. # Santas_present_factory
  225. from collections import deque
  226.  
  227. materials = deque(int(x) for x in input().split())
  228. magic_levels = deque(int(x) for x in input().split())
  229.  
  230. crafted = []
  231.  
  232. presents = {
  233.     150: "Doll",
  234.     250: "Wooden train",
  235.     300: "Teddy bear",
  236.     400: "Bicycle"
  237. }
  238.  
  239. while materials and magic_levels:
  240.     material = materials.pop() if magic_levels[0] or not materials[0] else 0
  241.     magic_levels = magic_levels.pop() if material or not magic_levels[0] else 0
  242.  
  243.     if not magic_levels:
  244.         continue
  245.  
  246.     product = material * magic_levels
  247.  
  248.     if presents.get(product):
  249.         crafted.append(presents[product])
  250.  
  251.     elif product < 0:
  252.         materials.append(material + magic_levels)
  253.     elif product > 0:
  254.         materials.append(material + 15)
  255.  
  256. if {"Doll", "Wooden train"}.issubset(crafted) or {"Teddy bear", "Bicycle"}.issubset(crafted):
  257.     print("The presents...")
  258. else:
  259.     print("No presents this Cristmas!")
  260.  
  261.  
  262. if materials:
  263.     print(f"Materials left: {', '.join(str(x) for x in materials[::-1])}")
  264.  
  265. if magic_levels:
  266.     print(f"Magic left: {', '.join([str(x) for x in magic_levels])}")
  267.  
  268. [print(f"{toy}: {crafted.count(toy)}") for toy in sorted(set(crafted))]
  269.  
  270.  
  271.  
  272.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement