<jupyter_start><jupyter_text>Code : Stack Using LL<jupyter_code>from sys import stdin

class Node :

	def __init__(self, data) :
		self.data = data
		self.next = None

class Stack :

	def __init__(self) :
		self.__head = None
		self.__size = 0


	def getSize(self) :
		return self.__size


	def isEmpty(self) :
		return self.__size == 0


	def push(self, data) :
		newNode = Node(data)

		if self.__head is None :
			self.__head = newNode

		else :
			newNode.next = self.__head
			self.__head= newNode

		self.__size += 1


	def pop(self) :
		if self.__head is None :
			return -1

		ans = self.__head.data

		self.__head = self.__head.next
		self.__size -= 1

		return ans


	def top(self) :
		if self.__head is None :
			return -1

		return self.__head.data

#main
q = int(stdin.readline().strip())

stack = Stack()

while q > 0 :

	inputs = stdin.readline().strip().split(" ")
	choice = int(inputs[0])

	if choice == 1 :
		data = int(inputs[1])
		stack.push(data)

	elif choice == 2 :
		print(stack.pop())

	elif choice == 3 :
		print(stack.top())

	elif choice == 4 : 
		print(stack.getSize())

	else :
		if stack.isEmpty() :
			print("true")
		else :
			print("false")

	q -= 1<jupyter_output><empty_output><jupyter_text>Balanced Paranthesis<jupyter_code>from sys import stdin


def isEmpty(stack) :
    return len(stack) == 0

def isBalanced(expression) :

        stack = list()

        for i in range(len(expression)) :

            if expression[i] == '(' :
                stack.append(expression[i])
            elif expression[i] == ')' :
                if isEmpty(stack) :
                    return False

                topChar = stack.pop();

                if expression[i] == ')' and topChar == '(' :
                    continue
                else :
                    return False
                
        return isEmpty(stack);




#main
expression = stdin.readline().strip()

if isBalanced(expression) :
    print("true")

else :
    print("false")<jupyter_output><empty_output><jupyter_text>Reverse Stack<jupyter_code>from sys import stdin, setrecursionlimit
setrecursionlimit(10 ** 6)

def reverseStack(inputStack, extraStack) :
    if len(inputStack) == 0:
        return;

    lastElement = inputStack.pop()

    reverseStack(inputStack, extraStack);

    while not isEmpty(inputStack) :
        top = inputStack.pop()
        extraStack.append(top)

    inputStack.append(lastElement)

    while not isEmpty(extraStack) :
        top = extraStack.pop()
        inputStack.append(top)



'''-------------- Utility Functions --------------'''

#Takes a list as a stack and returns whether the stack is empty or not
def isEmpty(stack) :
    return len(stack) == 0



#Taking input using fast I/o method
def takeInput() :
    size = int(stdin.readline().strip())
    inputStack = list()

    if size == 0 :
        return inputStack


    values = list(map(int, stdin.readline().strip().split(" ")))
    inputStack = values

    return inputStack


#main
inputStack = takeInput()
emptyStack = list()

reverseStack(inputStack, emptyStack)

while not isEmpty(inputStack) :
    print(inputStack.pop(), end = " ")<jupyter_output><empty_output><jupyter_text>Check redundant brackets<jupyter_code>from sys import stdin

def checkRedundantBrackets(expression) :
    stk = list()

    for i in range(len(expression)) :

        if (expression[i] == '(') or (find(expression[i])) :
            stk.append(expression[i])
            
        elif expression[i] == ')' :
            hasOperator = False
        
            while not isEmpty(stk) and top(stk) != '(' :
                stk.pop()
                hasOperator = True
                
            if not hasOperator : 
                return True
                
            if not isEmpty(stk) :
                stk.pop()

    return False



'''-------------- Utility Functions --------------'''

def find(ch) :
    if ch == '+' or ch == '-' or ch == '*' or ch == '/' :
        return True

    return False


#Takes a list as a stack and returns whether the stack is empty or not
def isEmpty(stack) :
    return len(stack) == 0


#Takes a list as a stack and returns the element at the top
def top(stack) :
    #assuming the stack is never empty
    return stack[len(stack) - 1]




#main
expression = stdin.readline().strip()

if checkRedundantBrackets(expression) :
    print("true")

else :
    print("false")<jupyter_output><empty_output><jupyter_text>Stock Span<jupyter_code>from sys import stdin

def stockSpan(price, n) :
    stk = list()
    output = [-1] * n

    stk.append(0)
    output[0] = 1

    for i in range(1, n) :
        while (not isEmpty(stk)) and (price[top(stk)] < price[i]) :
            stk.pop()

        if isEmpty(stk) :
            output[i] = i + 1
        else :
            output[i] = i - top(stk)

        stk.append(i)


    return output



'''-------------- Utility Functions --------------'''


#Takes a list as a stack and returns whether the stack is empty or not
def isEmpty(stack) :
    return len(stack) == 0


#Takes a list as a stack and returns the element at the top
def top(stack) :
    #assuming the stack is never empty
    return stack[len(stack) - 1]


def printList(arr) :
    for i in range(len(arr)) :
        print(arr[i], end = " ")

    print()


def takeInput():
    size = int(stdin.readline().strip())

    if size == 0 :
        return list(), 0

    price = list(map(int, stdin.readline().strip().split(" ")))

    return price, size


#main
price, n = takeInput()

output = stockSpan(price, n)
printList(output)<jupyter_output><empty_output><jupyter_text>Minimum bracket Reversal<jupyter_code>from sys import stdin

def countBracketReversals(inputString) :
    
    length = len(inputString)

    if length == 0 :
        return 0
    
    if length % 2 != 0 :
        return -1  # Only even number of brackets can be balanced


    stack = list()

    for i in range(length) :
        currentChar = inputString[i]

        if currentChar == '{' :
            stack.append(currentChar)
        else :
            # Pop if there is a balanced pair
            if (not isEmpty(stack)) and (top(stack) == '{') :
                stack.pop()
            else :
                stack.append(currentChar)

    count = 0

    #Only unbalanced brackets are there in stack now
    while not isEmpty(stack) :
        char1 = stack.pop()
        char2 = stack.pop()

        '''
            When char1 = } and char2 = {, then we need to reverse both of them 
            so count will increase by 2
        '''
        if char1 != char2 :
            count += 2
        else :
            count += 1

    return count




'''-------------- Utility Functions --------------'''


#Takes a list as a stack and returns whether the stack is empty or not
def isEmpty(stack) :
    return len(stack) == 0


#Takes a list as a stack and returns the element at the top
def top(stack) :
    #assuming the stack is never empty
    return stack[len(stack) - 1]


#main
print(countBracketReversals(stdin.readline().strip()))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1298205961293968, 'fraction_numerical': 0.007063144511936714, 'mean_word_length': 2.1877532642953623, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '97909', 'license': 'MIT', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2169, 'n_words': 626, 'path': 'Coding-Ninjas-Data-Structure-and-Algorithm-in-Python-main/Stack/All codes in one.ipynb', 'repo_name': 'SaiPrasanth212/Coding-Ninjas-Data-Structures-and-Algorithms', 'size': '7079'}