Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python-Advanced-Exercises , Lists as Stacks and Queues - Exercise
- # https://judge.softuni.org/Contests/Practice/Index/1831#0
- # 01. Reverse Numbers
- # 02. Stacked Queries
- # 03. Fast Food
- # 04. Fashion Boutique
- # 05. Truck Tour
- # 06. Balanced Parentheses
- # 07. *Robotics
- # 08. *Crossroads
- # 09. *Key Revolver
- # 10. *Cups and Bottles
- ======================================================================================================================
- # 01. Reverse Numbers
- data = list(map(int, input().split()))
- for _ in range(len(data)):
- removed_element = data.pop()
- print(removed_element, end=" ")
- ======================================================================================================================
- #02. Stacked Queries
- my_stack = []
- n = int(input())
- for _ in range(n):
- current_input = [int(x) for x in input().split()]
- if len(current_input) > 1:
- current_num = current_input[1]
- my_stack.append(current_num)
- else:
- type_num = current_input[0]
- if type_num == 2 and my_stack:
- my_stack.pop()
- elif type_num == 3 and my_stack:
- print(max(my_stack))
- elif type_num == 4 and my_stack:
- print(min(my_stack))
- final_print_stack = []
- for _ in range(len(my_stack)):
- popped_el = my_stack.pop()
- final_print_stack.append(popped_el)
- print(*final_print_stack, sep=', ')
- ======================================================================================================================
- # 03. Fast Food
- from collections import deque
- total_food = int(input())
- all_orders = deque([int(x) for x in input().split()])
- biggest_order = max(all_orders)
- print(biggest_order)
- while all_orders:
- completed = False
- for order in all_orders:
- if total_food >= order:
- all_orders.popleft()
- total_food -= order
- break
- else:
- completed = True
- break
- if completed:
- break
- if not all_orders:
- print('Orders complete')
- else:
- print(f'Orders left: {" ".join([str(x) for x in all_orders])}')
- ======================================================================================================================
- # 04. Fashion Boutique
- clothes_stack = [int(x) for x in input().split()]
- default_cap = int(input())
- rack_cap = default_cap
- count_racks = 1
- while clothes_stack:
- last_cloth = clothes_stack[-1]
- if last_cloth <= rack_cap:
- rack_cap -= last_cloth
- clothes_stack.pop()
- else:
- rack_cap = default_cap
- count_racks += 1
- print(count_racks)
- ======================================================================================================================
- # 05. Truck Tour
- #1 var1
- from collections import deque
- petrol_pumps = int(input())
- pumps = deque()
- original_pumps = deque()
- for pump in range(petrol_pumps):
- pump_details = [int(x) for x in input().split()]
- pumps.append(pump_details)
- original_pumps.append(pump_details)
- count_stops = 0
- total_liters = 0
- while pumps:
- if count_stops == len(pumps):
- first_start = pumps[0]
- start_position = original_pumps.index(first_start)
- print(start_position)
- break
- for position in range(petrol_pumps):
- liters = pumps[position][0]
- kms = pumps[position][1]
- total_liters += liters
- if total_liters >= kms:
- total_liters -= kms
- count_stops += 1
- else:
- rotated = pumps.popleft()
- pumps.append(rotated)
- count_stops = 0
- total_liters = 0
- break
- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- #2 var2
- from collections import deque
- n = int(input())
- pumps = deque()
- for _ in range(n):
- pump_details = [int(x) for x in input().split()]
- pumps.append(pump_details)
- for starts in range(n):
- tank = 0
- failed = False
- for details in pumps:
- fuel = details[0]
- kms = details[1]
- tank += fuel
- if kms > tank:
- failed = True
- break
- else:
- tank -= kms
- if failed:
- removed = pumps.popleft()
- pumps.append(removed)
- else:
- print(starts)
- break
- ======================================================================================================================
- # 06. Balanced Parentheses
- sequence = input()
- balanced = False
- brackets = []
- for el in sequence:
- if el in '({[':
- brackets.append(el)
- elif el in ')}]' and brackets:
- popped_last = brackets.pop()
- if popped_last + el in '()[]{}':
- balanced = True
- else:
- balanced = False
- break
- else:
- balanced = False
- break
- if balanced and not brackets:
- print('YES')
- else:
- print('NO')
- ======================================================================================================================
- # 07. *Robotics
- from collections import deque
- def convert_str_to_seconds(str_time):
- hours, minutes, seconds = [int(x) for x in str_time.split(':')]
- return hours * 60 * 60 + minutes * 60 + seconds
- def convert_seconds_to_str_time(seconds):
- seconds %= 24 * 60 * 60
- hours = seconds // (60 * 60)
- seconds %= (60 * 60)
- minutes = seconds // 60
- seconds %= 60
- return f'{hours:02d}:{minutes:02d}:{seconds:02d}'
- robots_data = input().split(';')
- process_time_by_robot = {}
- busy_until_by_robot = {}
- for robot_data in robots_data:
- name, process_time = robot_data.split('-')
- process_time_by_robot[name] = int(process_time)
- busy_until_by_robot[name] = -1
- time = convert_str_to_seconds(input())
- items = deque()
- while True:
- line = input()
- if line == 'End':
- break
- items.append(line)
- while items:
- time = (time + 1)
- item = items.popleft()
- for name, busy_until in busy_until_by_robot.items():
- if time >= busy_until:
- busy_until_by_robot[name] = time + process_time_by_robot[name]
- print(f'{name} - {item} [{convert_seconds_to_str_time(time)}]')
- break
- else:
- items.append(item)
- ======================================================================================================================
- # 08. *Crossroads
- from collections import deque
- green_light = int(input())
- free_window = int(input())
- crashed = False
- time = green_light
- extra_time = free_window
- waiting_cars = deque()
- passed_safely = 0
- first_car = None
- while True:
- time, extra_time = green_light, free_window # reset the time !
- command = input()
- if command == 'END':
- break
- if command == 'green':
- while waiting_cars:
- first_car = waiting_cars[0]
- if len(first_car) <= time: # car passed safely on green light time
- passed_safely += 1
- time -= len(first_car)
- elif time + extra_time >= len(first_car): # car passed safely on extra time
- passed_safely += 1
- extra_time = (time + extra_time) - len(first_car)
- time = 0
- else: # not even enter the cross road -> break
- if time: # crash -> break
- crashed = True
- break
- waiting_cars.popleft() # after safely passed -> remove car from queue
- if crashed: # if crashed , stop all loops
- break
- continue
- waiting_cars.append(command)
- if crashed:
- index = time + extra_time # index of crashed letter
- print('A crash happened!')
- print(f'{first_car} was hit at {first_car[index]}.')
- else:
- print('Everyone is safe.')
- print(f'{passed_safely} total cars passed the crossroads.')
- ======================================================================================================================
- # 09. *Key Revolver
- from collections import deque
- price_per_bullet = int(input())
- initial_barrel_size = int(input())
- barrel_size = initial_barrel_size
- bullets = deque([int(x) for x in input().split()]) # LIFO -> starts from index[-1]
- locks = deque([int(x) for x in input().split()]) # FIFO -> starts from index[0]
- value = int(input())
- total_bullets = 0
- while True:
- if not bullets or not locks:
- break
- last_bullets = bullets.pop()
- first_lock = locks[0]
- total_bullets += 1
- if last_bullets <= first_lock:
- print('Bang!')
- locks.popleft()
- else:
- print('Ping!')
- barrel_size -= 1
- if barrel_size == 0 and bullets:
- print('Reloading!')
- barrel_size = initial_barrel_size
- if not locks:
- print(f"{len(bullets)} bullets left. Earned ${value - (total_bullets * price_per_bullet)}")
- else:
- print(f"Couldn't get through. Locks left: {len(locks)}")
- ======================================================================================================================
- # 10. *Cups and Bottles
- from collections import deque
- cups = deque([int(x) for x in input().split()]) # FIFO -> start from first index[0]
- bottles = deque([int(x) for x in input().split()]) # LIFO -> start from last index[-1]
- wasted_water = 0
- while True:
- if not cups or not bottles:
- break
- first_cup = cups[0]
- last_bottle = bottles.pop()
- if last_bottle >= first_cup:
- wasted_water += last_bottle - first_cup
- cups.popleft()
- else:
- cups[0] -= last_bottle
- if first_cup <= 0:
- wasted_water += abs(first_cup)
- cups.popleft()
- if not cups:
- print(f"Bottles: {' '.join(str(x) for x in bottles)} ")
- if not bottles:
- print(f"Cups: {' '.join(str(x) for x in cups)}")
- print(f'Wasted litters of water: {wasted_water}')
- ======================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement