def stack_rev(input_stack): temp_stack = Stack() while not input_stack.is_empty(): temp = input_stack.pop() while not temp_stack.is_empty() and temp_stack.peek() > temp: input_stack.push(temp_stack.pop()) temp_stack.push(temp) return temp_stack
def postfixEval(postfixExpr): operandStack = Stack() tokenList = postfixExpr.split() for token in tokenList: if token in "0123456789": operandStack.push(int(token)) else: operand2 = operandStack.pop() operand1 = operandStack.pop() result = do_math(token, operand1, operand2) operandStack.push(result) return operandStack.pop()
def dec_bin(num): s = Stack() while num >0: reminder = num % 2 s.push(reminder) num = num // 2 bindec = "" while not s.is_empty(): bindec += str(s.pop()) return bindec
def rev(value): s = Stack() for ch in value: s.push(ch) revstk = '' while not s.is_empty(): revstk += s.pop() if (value == revstk): print('Given string is palidrome') else: print('Given string is not palindrome') return revstk
from classtak import Stack s1 = Stack() s2 = Stack() s3 = Stack() s1.push(3) s1.push(2) s1.push(1) print(s2.push(s1.pop())) print(s3.push(s1.pop())) print(s3.push(s2.pop())) print(s2.push(s1.pop())) print(s1.push(s3.pop())) print(s2.push(s3.pop())) print(s2.push(s1.pop()))
def infix_postfix(exp): prec = {} prec["*"] = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 prec["("] = 1 s = Stack() postfix_list = [] infix_list = exp.split() for ch in infix_list: if ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or ch in "0123456789": postfix_list.append(ch) elif ch == "(": s.push(ch) elif ch == ")": top_element = s.pop() while top_element != "(": postfix_list.append(top_element) top_element = s.pop() else: while (not s.is_empty()) and (prec[s.peek()] >= prec[ch]): postfix_list.append(s.pop()) s.push(ch) while (not s.is_empty()): postfix_list.append(s.pop()) return " ".join(postfix_list)
from classtak import Stack s = Stack() def conv_string(num, base): string_conv = "0123456789ABCDEF" while num > 0: if num < base: s.push(string_conv[num]) else: s.push(string_conv[num % base]) num = num // base res = "" while not s.is_empty(): res = res + str(s.pop()) return res print(conv_string(10, 2))
from classtak import Stack def stack_rev(input_stack): temp_stack = Stack() while not input_stack.is_empty(): temp = input_stack.pop() while not temp_stack.is_empty() and temp_stack.peek() > temp: input_stack.push(temp_stack.pop()) temp_stack.push(temp) return temp_stack s = Stack() s.push(31) s.push(3) s.push(15) s.push(98) s.push(92) s.push(23) print(stack_rev(s))