Advertisement
GeorgiLukanov87

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

Aug 17th, 2022 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. # Python-Advanced-Exercises , Lists as Stacks and Queues - Lab
  2. # https://judge.softuni.org/Contests/Practice/Index/1830#0
  3.  
  4. # 01. Reverse Strings
  5. # 02. Matching Parentheses
  6. # 03. Supermarket
  7. # 04. Water Dispenser
  8. # 05. Hot Potato
  9. ================================================================================================================
  10. # 01. Reverse Strings
  11.  
  12. data = list(input())
  13. while data:
  14.     print(data.pop(), end='')
  15.  
  16.  
  17. ================================================================================================================
  18. # 02. Matching Parentheses
  19.  
  20. expression = input()
  21. par_stack = []
  22.  
  23. for index in range(len(expression)):
  24.     if expression[index] == '(':
  25.         par_stack.append(index)
  26.  
  27.     elif expression[index] == ')':
  28.         start_index = par_stack.pop()
  29.         print(expression[start_index:index+1])
  30.  
  31.  
  32. ================================================================================================================
  33. # 03. Supermarket
  34.  
  35.  
  36. from collections import deque
  37. name = input()
  38. line = deque()
  39.  
  40. while not name == 'End':
  41.     if name == 'Paid':
  42.         while line:
  43.             print(line.popleft())
  44.     else:
  45.         line.append(name)
  46.     name = input()
  47.  
  48. print(f'{len(line)} people remaining.')
  49.  
  50.  
  51. ================================================================================================================
  52. # 04. Water Dispenser
  53.  
  54.  
  55. from collections import deque
  56. liters = int(input())
  57. name = input()
  58. line = deque()
  59.  
  60. while not name == 'Start':
  61.     line.append(name)
  62.     name = input()
  63.  
  64. command = input()
  65. while not command == 'End':
  66.     if command.isdigit():
  67.         required_liters = int(command)
  68.         name = line.popleft()
  69.         if liters >= required_liters:
  70.             liters -= required_liters
  71.             print(f'{name} got water')
  72.         else:
  73.             print(f'{name} must wait')
  74.     else:
  75.         command = command.split()
  76.         litters_to_fill = int(command[1])
  77.         liters += litters_to_fill
  78.  
  79.     command = input()
  80.  
  81. print(f'{liters} liters left')
  82.  
  83.  
  84. ================================================================================================================
  85. # 05. Hot Potato
  86.  
  87.  
  88. from collections import deque
  89.  
  90. kids = deque(input().split())
  91. n = int(input())
  92. while len(kids) > 1:
  93.     kids.rotate(-n)
  94.     print(f"Removed {kids.pop()}")
  95.  
  96. print(f'Last is {kids.pop()}')
  97.  
  98. ================================================================================================================
  99.  
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement