Untitled

mail@pastecode.io avatar
unknown
python
a year ago
2.2 kB
1
Indexable
Never
class Stack:
    def __init__(self, capacity):
        self.capacity = capacity
        self.stack = []

    def push(self, x):
        if len(self.stack) == self.capacity:
            raise Exception('Stack overflow')
        self.stack.append(x)

    def pop(self):
        if self.empty():
            raise Exception('Stack underflow')
        return self.stack.pop()

    def clear(self):
        self.stack.clear()

    def empty(self):
        return len(self.stack) == 0

    def size(self):
        return len(self.stack)

    def top(self):
        if self.empty():
            raise Exception('Stack is empty')
        return self.stack[-1]

#zad 1
def reverse1(string:str):
    stack = Stack(len(string))
    result_str=''
    for i in string:
        stack.push(i)
    while not stack.empty():
        result_str+=stack.pop()
    return result_str
#zad 2

def posledovatelnost(string:str):
    stack = Stack(len(string))
    for char in string:
        if char in "([{":
            stack.push(char)
        elif char in ")]}":
            if stack.empty():
                return False
            top_char = stack.pop()
            if (char == ")" and top_char != "(") or (char == "]" and top_char != "[") or (char == "}" and top_char != "{"):
                return False
    return stack.empty()


#zad 3
def vagonSort(list_vagon:list[int]):
    stack = Stack(len(list_vagon))
    sorted_vagon=[]
    list_res=[]
    stack.push(list_vagon[0])
    list_res.append('+1')
    for i in range(1,len(list_vagon)):
        if list_vagon[i]>stack.top():
            while not stack.empty() and stack.top()<list_vagon[i]:
                sorted_vagon.append(stack.pop())
                list_res.append('-1')
            stack.push(list_vagon[i])
            list_res.append('+1')
        else:
            stack.push(list_vagon[i])
            list_res.append('+1')
    while not stack.empty():
        sorted_vagon.append(stack.pop())
        list_res.append('-1')
    if sorted_vagon!=sorted(list_vagon):
        print(0)
    else:
        for _ in list_res:
            print(_)


if __name__=='__main__':
    vagonSort([1,4,5,3,2])