7 months ago
23 lines489 B
from collections import deque | |
| |
PAIRS = {")": "(", "]": "[", "}": "{"} | |
| |
| |
def balanced(text): | |
stack = [] | |
for ch in text: | |
if ch in "([{": | |
stack.append(ch) | |
elif ch in PAIRS: | |
if not stack or stack.pop() != PAIRS[ch]: | |
return False | |
return not stack | |
| |
| |
print(balanced("{[()()]}"), balanced("(]")) | |
| |
queue = deque(["first", "second"]) | |
queue.append("third") | |
print(queue.popleft(), list(queue)) | |
| |
# Questions? Ask in the course forum. |