Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.7 kB
2
Indexable
Never
class Stack:
    def __init__(self, capacity):
        self.capacity = capacity
        self.stack = [None] * capacity
        self.sizee = 0

    def push(self, x):  # добавить
        if self.sizee == self.capacity:
            raise Exception('Stack overflow')
        self.stack[self.sizee] = x
        self.sizee += 1

    def pop(self):  # удалить
        if self.empty():
            raise Exception('Stack underflow')
        temp = self.stack[self.sizee]
        self.stack[self.sizee] = None
        self.sizee -= 1
        return temp

    def clear(self):
        self.capacity = self.capacity
        self.stack = [None] * self.capacity
        self.sizee = 0

    def empty(self) -> bool:
        return self.sizee == 0

    def size(self):
        return self.sizee

    def top(self):
        if self.empty():
            raise Exception('Stack is empty')
        temp = self.stack[self.sizee]
        return temp


def zad2(a):
    st = Stack(len(a))
    resault = ""
    for i in range(len(a)):
        st.push(a[i])
    for i in range(len(a)):
        resault += st.pop()
    print(resault)


def zad3(a):
    st = Stack(len(a))
    for i in a:
        if i in '([{':
            st.push(i)
        elif i in ')]}':
            if st.empty():
                return False
            open_brackets = st.pop()
            if open_brackets == '(' and i == ')':
                continue
            if open_brackets == '[' and i == ']':
                continue
            if open_brackets == '{' and i == '}':
                continue
            return False
    return st.empty()


zad2("hello")
print(zad3("()()"))