Advertisement
GeorgiLukanov87

Python-Advanced-Exercises , Lists as Stacks and Queues - Exercise

Aug 17th, 2022 (edited)
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.08 KB | None | 0 0
  1. # Python-Advanced-Exercises , Lists as Stacks and Queues - Exercise
  2. # https://judge.softuni.org/Contests/Practice/Index/1831#0
  3.  
  4. # 01. Reverse Numbers
  5. # 02. Stacked Queries
  6. # 03. Fast Food
  7. # 04. Fashion Boutique
  8. # 05. Truck Tour
  9. # 06. Balanced Parentheses
  10. # 07. *Robotics
  11. # 08. *Crossroads
  12. # 09. *Key Revolver
  13. # 10. *Cups and Bottles
  14.  
  15. ======================================================================================================================
  16. # 01. Reverse Numbers
  17.  
  18. data = list(map(int, input().split()))
  19. for _ in range(len(data)):
  20.     removed_element = data.pop()
  21.     print(removed_element, end=" ")
  22.  
  23.    
  24. ======================================================================================================================
  25. #02. Stacked Queries
  26.  
  27.  
  28. my_stack = []
  29. n = int(input())
  30.  
  31. for _ in range(n):
  32.     current_input = [int(x) for x in input().split()]
  33.     if len(current_input) > 1:
  34.         current_num = current_input[1]
  35.         my_stack.append(current_num)
  36.     else:
  37.         type_num = current_input[0]
  38.         if type_num == 2 and my_stack:
  39.             my_stack.pop()
  40.  
  41.         elif type_num == 3 and my_stack:
  42.             print(max(my_stack))
  43.  
  44.         elif type_num == 4 and my_stack:
  45.             print(min(my_stack))
  46.  
  47. final_print_stack = []
  48. for _ in range(len(my_stack)):
  49.     popped_el = my_stack.pop()
  50.     final_print_stack.append(popped_el)
  51.  
  52. print(*final_print_stack, sep=', ')
  53.  
  54.  
  55. ======================================================================================================================
  56. # 03. Fast Food
  57.  
  58. from collections import deque
  59.  
  60. total_food = int(input())
  61. all_orders = deque([int(x) for x in input().split()])
  62.  
  63. biggest_order = max(all_orders)
  64. print(biggest_order)
  65.  
  66. while all_orders:
  67.     completed = False
  68.     for order in all_orders:
  69.         if total_food >= order:
  70.             all_orders.popleft()
  71.             total_food -= order
  72.             break
  73.         else:
  74.             completed = True
  75.             break
  76.            
  77.     if completed:
  78.         break
  79.  
  80. if not all_orders:
  81.     print('Orders complete')
  82. else:
  83.     print(f'Orders left: {" ".join([str(x) for x in all_orders])}')
  84.    
  85.    
  86. ======================================================================================================================
  87. # 04. Fashion Boutique
  88.  
  89.  
  90. clothes_stack = [int(x) for x in input().split()]
  91. default_cap = int(input())
  92. rack_cap = default_cap
  93. count_racks = 1
  94. while clothes_stack:
  95.     last_cloth = clothes_stack[-1]
  96.     if last_cloth <= rack_cap:
  97.         rack_cap -= last_cloth
  98.         clothes_stack.pop()
  99.     else:
  100.         rack_cap = default_cap
  101.         count_racks += 1
  102.  
  103. print(count_racks)
  104.  
  105.  
  106. ======================================================================================================================
  107. # 05. Truck Tour
  108.  
  109.  
  110. #1 var1
  111.  
  112. from collections import deque
  113. petrol_pumps = int(input())
  114. pumps = deque()
  115. original_pumps = deque()
  116.  
  117. for pump in range(petrol_pumps):
  118.     pump_details = [int(x) for x in input().split()]
  119.     pumps.append(pump_details)
  120.     original_pumps.append(pump_details)
  121.  
  122. count_stops = 0
  123. total_liters = 0
  124.  
  125. while pumps:
  126.     if count_stops == len(pumps):
  127.         first_start = pumps[0]
  128.         start_position = original_pumps.index(first_start)
  129.         print(start_position)
  130.         break
  131.  
  132.     for position in range(petrol_pumps):
  133.         liters = pumps[position][0]
  134.         kms = pumps[position][1]
  135.         total_liters += liters
  136.         if total_liters >= kms:
  137.             total_liters -= kms
  138.             count_stops += 1
  139.         else:
  140.             rotated = pumps.popleft()
  141.             pumps.append(rotated)
  142.             count_stops = 0
  143.             total_liters = 0
  144.             break
  145.  
  146. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
  147.  
  148.  #2 var2
  149.  
  150. from collections import deque
  151.  
  152. n = int(input())
  153. pumps = deque()
  154.  
  155. for _ in range(n):
  156.     pump_details = [int(x) for x in input().split()]
  157.     pumps.append(pump_details)
  158.  
  159. for starts in range(n):
  160.     tank = 0
  161.     failed = False
  162.  
  163.     for details in pumps:
  164.         fuel = details[0]
  165.         kms = details[1]
  166.         tank += fuel
  167.         if kms > tank:
  168.             failed = True
  169.             break
  170.         else:
  171.             tank -= kms
  172.  
  173.     if failed:
  174.         removed = pumps.popleft()
  175.         pumps.append(removed)
  176.     else:
  177.         print(starts)
  178.         break
  179.        
  180.        
  181. ======================================================================================================================
  182. # 06. Balanced Parentheses
  183.          
  184.    
  185. sequence = input()
  186. balanced = False
  187. brackets = []
  188.  
  189. for el in sequence:
  190.     if el in '({[':
  191.         brackets.append(el)
  192.     elif el in ')}]' and brackets:
  193.         popped_last = brackets.pop()
  194.         if popped_last + el in '()[]{}':
  195.             balanced = True
  196.         else:
  197.             balanced = False
  198.             break
  199.     else:
  200.         balanced = False
  201.         break
  202.  
  203. if balanced and not brackets:
  204.     print('YES')
  205. else:
  206.     print('NO')
  207.  
  208.    
  209. ======================================================================================================================
  210. # 07. *Robotics
  211.  
  212.  
  213. from collections import deque
  214.  
  215.  
  216. def convert_str_to_seconds(str_time):
  217.     hours, minutes, seconds = [int(x) for x in str_time.split(':')]
  218.     return hours * 60 * 60 + minutes * 60 + seconds
  219.  
  220.  
  221. def convert_seconds_to_str_time(seconds):
  222.     seconds %= 24 * 60 * 60
  223.  
  224.     hours = seconds // (60 * 60)
  225.     seconds %= (60 * 60)
  226.     minutes = seconds // 60
  227.     seconds %= 60
  228.     return f'{hours:02d}:{minutes:02d}:{seconds:02d}'
  229.  
  230.  
  231. robots_data = input().split(';')
  232. process_time_by_robot = {}
  233. busy_until_by_robot = {}
  234.  
  235. for robot_data in robots_data:
  236.     name, process_time = robot_data.split('-')
  237.     process_time_by_robot[name] = int(process_time)
  238.     busy_until_by_robot[name] = -1
  239.  
  240. time = convert_str_to_seconds(input())
  241. items = deque()
  242.  
  243. while True:
  244.     line = input()
  245.     if line == 'End':
  246.         break
  247.     items.append(line)
  248.  
  249. while items:
  250.     time = (time + 1)
  251.     item = items.popleft()
  252.  
  253.     for name, busy_until in busy_until_by_robot.items():
  254.         if time >= busy_until:
  255.             busy_until_by_robot[name] = time + process_time_by_robot[name]
  256.             print(f'{name} - {item} [{convert_seconds_to_str_time(time)}]')
  257.             break
  258.     else:
  259.         items.append(item)
  260.        
  261.        
  262. ======================================================================================================================
  263.  
  264. # 08. *Crossroads
  265.  
  266.  
  267. from collections import deque
  268.  
  269. green_light = int(input())
  270. free_window = int(input())
  271. crashed = False
  272. time = green_light
  273. extra_time = free_window
  274.  
  275. waiting_cars = deque()
  276. passed_safely = 0
  277. first_car = None
  278. while True:
  279.     time, extra_time = green_light, free_window  # reset the time !
  280.     command = input()
  281.     if command == 'END':
  282.         break
  283.     if command == 'green':
  284.         while waiting_cars:
  285.             first_car = waiting_cars[0]
  286.  
  287.             if len(first_car) <= time:  # car passed safely on green light time
  288.                 passed_safely += 1
  289.                 time -= len(first_car)
  290.  
  291.             elif time + extra_time >= len(first_car):  # car passed safely on extra time
  292.                 passed_safely += 1
  293.                 extra_time = (time + extra_time) - len(first_car)
  294.                 time = 0
  295.  
  296.             else:  # not even enter the cross road -> break
  297.                 if time:  # crash -> break
  298.                     crashed = True
  299.                 break
  300.  
  301.             waiting_cars.popleft()  # after safely passed -> remove car from queue
  302.  
  303.         if crashed:  # if crashed , stop all loops
  304.             break
  305.  
  306.         continue
  307.  
  308.     waiting_cars.append(command)
  309.  
  310. if crashed:
  311.     index = time + extra_time  # index of crashed letter
  312.     print('A crash happened!')
  313.     print(f'{first_car} was hit at {first_car[index]}.')
  314. else:
  315.     print('Everyone is safe.')
  316.     print(f'{passed_safely} total cars passed the crossroads.')
  317.  
  318.    
  319. ======================================================================================================================
  320.  
  321. # 09. *Key Revolver
  322.  
  323.  
  324. from collections import deque
  325.  
  326. price_per_bullet = int(input())
  327. initial_barrel_size = int(input())
  328. barrel_size = initial_barrel_size
  329.  
  330. bullets = deque([int(x) for x in input().split()])  # LIFO -> starts from index[-1]
  331. locks = deque([int(x) for x in input().split()])  # FIFO -> starts from index[0]
  332. value = int(input())
  333. total_bullets = 0
  334. while True:
  335.     if not bullets or not locks:
  336.         break
  337.  
  338.     last_bullets = bullets.pop()
  339.     first_lock = locks[0]
  340.     total_bullets += 1
  341.  
  342.     if last_bullets <= first_lock:
  343.         print('Bang!')
  344.         locks.popleft()
  345.     else:
  346.         print('Ping!')
  347.  
  348.     barrel_size -= 1
  349.     if barrel_size == 0 and bullets:
  350.         print('Reloading!')
  351.         barrel_size = initial_barrel_size
  352.  
  353. if not locks:
  354.     print(f"{len(bullets)} bullets left. Earned ${value - (total_bullets * price_per_bullet)}")
  355. else:
  356.     print(f"Couldn't get through. Locks left: {len(locks)}")
  357.  
  358.    
  359. ======================================================================================================================
  360.  
  361. # 10. *Cups and Bottles
  362.  
  363.  
  364. from collections import deque
  365.  
  366. cups = deque([int(x) for x in input().split()])  # FIFO -> start from first index[0]
  367. bottles = deque([int(x) for x in input().split()])  # LIFO -> start from last index[-1]
  368. wasted_water = 0
  369. while True:
  370.     if not cups or not bottles:
  371.         break
  372.  
  373.     first_cup = cups[0]
  374.     last_bottle = bottles.pop()
  375.  
  376.     if last_bottle >= first_cup:
  377.         wasted_water += last_bottle - first_cup
  378.         cups.popleft()
  379.     else:
  380.         cups[0] -= last_bottle
  381.         if first_cup <= 0:
  382.             wasted_water += abs(first_cup)
  383.             cups.popleft()
  384.  
  385. if not cups:
  386.     print(f"Bottles: {' '.join(str(x) for x in bottles)} ")
  387. if not bottles:
  388.     print(f"Cups: {' '.join(str(x) for x in cups)}")
  389. print(f'Wasted litters of water: {wasted_water}')
  390.  
  391.  
  392. ======================================================================================================================
  393.  
  394.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement