# zad 2 class Stos: def __init__(self): self.s = [] def push(self, x): # assert self.size() < 20, "Too many elements" self.s.append(x) def pop(self): assert not self.is_empty(), "Stack is empty" return self.s.pop(-1) def top(self): assert not self.is_empty(), "Stack is empty" return self.s[-1] def size(self): return len(self.s) def is_empty(self): return not bool(len(self.s)) def ciag_nawiasowy_poprawny(s): """Zwraca 'TAK' jesli podany ciag nawiasowy (string) jest poprawny, w przeciwnym wypadku 'NIE'.""" nawiasy = { "{": "}", "[": "]", "(": ")", "<": ">", } ciag = Stos() for c in s: if c in nawiasy: ciag.push(c) elif nawiasy[ciag.top()] == c: ciag.pop() else: return "NIE" if ciag.is_empty(): return "TAK" return "NIE" print(ciag_nawiasowy_poprawny("{{[()()]}}")) # TAK print(ciag_nawiasowy_poprawny("([)()]")) # NIE print(ciag_nawiasowy_poprawny("([(){}])[]")) # TAK print(ciag_nawiasowy_poprawny("([(){}]>")) # NIE print(ciag_nawiasowy_poprawny("([(){}>)")) # NIE print(ciag_nawiasowy_poprawny("([(]))")) # NIE