avp26/u6/matching_brackets.py
2026-10-13 14:51:14 +02:00

12 lines
580 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def is_paired(input_string):
zatvorky = {'(': ')', '[': ']', '{': '}'}
stack = []
for char in input_string: ## .append() добавляет в конец, а .pop() забирает из конца
if char in zatvorky:
stack.append(char)
elif char in zatvorky.values():
if not stack or zatvorky[stack.pop()] != char: # не пустой ли stack и совпадает
return False # ли последняя открывающая скобка с текущей закрывающей
return not stack