def postfix_verbose(string): """ Evaluate the postfix string, using a stack. Elements must be separated by spaces. Display progress on screen. """ tokenlist = string.split() stack = Stack() for token in tokenlist: print('next token taken from string is', token) if token in ["+", "-", "*", "/"]: second = stack.pop() first = stack.pop() print(' Op, so pop twice, and evaluate <2nd-top> <op> <top>', first, token, second) if token == "+": print(' = ', first + second) stack.push(first + second) elif token == "-": print(' = ', first - second) stack.push(first - second) elif token == "*": print(' = ', first * second) stack.push(first * second) else: print(' = ', first / second) stack.push(first / second) print(' and push back onto stack') else: print(' Value (=', token, ') so push onto stack') stack.push(int(token)) print('Stack is now:', stack) print('No tokens left in string, so pop from stack (of length 1):', stack) return stack.pop()
def infixConverter(equationString): numStack = Stack() numList = [] # operatorValues = {"/":2, "*":2, "+":1, "-":1} for variable in equationString: try: int(variable) numList.append(variable) except ValueError: if variable == "(": numStack.push(variable) elif variable == ")": while numStack.top() != "(": numList.append(numStack.pop()) numStack.pop() elif (variable == "*" or variable == "/"): if numStack.top() == "*" or numStack.top() == "/": numList.append(numStack.pop()) numStack.push(variable) else: numStack.push(variable) elif (variable == "+" or variable == "-"): numStack.push(variable) while numStack.length() != 0: numList.append(numStack.pop()) s = " " print(s.join(numList))
def queue_reverser(queue): new_queue = Queue() stack = Stack() length = queue.length for _ in range(length): item = queue.dequeue() stack.push(item) queue.enqueue(item) for _ in range(length): new_queue.enqueue(stack.pop()) return new_queue
def postfix(string): """ Evaluate the postfix string, using a stack. Elements must be separated by spaces. """ tokenlist = string.split() stack = Stack() for token in tokenlist: if token in ["+", "-", "*", "/"]: second = stack.pop() first = stack.pop() if token == "+": stack.push(first + second) elif token == "-": stack.push(first - second) elif token == "*": stack.push(first * second) else: stack.push(first / second) else: stack.push(int(token)) return stack.pop()
def reverse_queue(queue): """ Reverse a queue. Note that this will destroy the original queue. Args: queue - a queue Returns the reverse of the input queue. """ outqueue = QueueV3() stack = Stack() while not queue.length() == 0: item = queue.dequeue() stack.push(item) print('dequeued', item) while not stack.length() == 0: item = stack.pop() outqueue.enqueue(item) print('enqueued', item) return outqueue
def main(): print('stackReverser\n') stack1 = Stack() alpha = ['a', 'b', 'c', 'd', 'e'] alpha = [1, 2, 3] for i in range(len(alpha)): stack1.push(alpha[i]) rev_stack = stackReverser(stack1) print(rev_stack) for i in range(stack1.length()): print(stack1.pop()) for i in range(rev_stack.length()): print(rev_stack.pop()) print(rev_stack) print('bracketChecker\n') test_string = '[he{}ll(ooo)oooooo(o)]' print(bracketChecker(test_string))
def reverse_queue2(queue): """ Reverse a queue. This will finish with the original queue in its original state. Args: queue - a queue Returns the reverse of the input queue. """ outqueue = QueueV3() stack = Stack() i = queue.length() for pos in range(i): item = queue.dequeue() stack.push(item) print('dequeued', item) queue.enqueue(item) while not stack.length() == 0: item = stack.pop() outqueue.enqueue(item) print('enqueued', item) return outqueue
def palindrome_check_list(inlist): """ Determine whether inlist is a palindrome, using a stack. """ size = len(inlist) mid = size // 2 even = True if size % 2 == 1: even = False print('Midpoint is', mid, '; inlist is of EVEN length:', even) stack = Stack() pos = 0 while pos < mid: stack.push(inlist[pos]) pos = pos + 1 if not even: pos = pos + 1 while pos < len(inlist): print('mylist[', pos, ']=', inlist[pos], '; stack.top() =', stack.top()) #if inlist[pos] != stack.pop(): if not inlist[pos].is_equal(stack.pop()): return False pos = pos + 1 return True
def correctBracket(givenString): charStack = Stack() for char in givenString: if char == '{' or char == '[' or char == '(': charStack.push(char) elif char == '}': if charStack.top() != '{': return 'INCORRECT' charStack.pop() elif char == ']': if charStack.top() != '[': return 'INCORRECT' charStack.pop() elif char == ')': if charStack.top() != '(': return 'INCORRECT' charStack.pop() return 'ITS CORRECT'
def palindrome_check(string): """ Determine whether string is a palindrome, using a stack. """ mylist = list(string) size = len(mylist) mid = size // 2 even = True if size % 2 == 1: even = False print('Midpoint is', mid, '; string is of EVEN length:', even) stack = Stack() #create and return a stack object pos = 0 while pos < mid: #push first half of the string onto the stack stack.push(mylist[pos]) pos = pos + 1 if not even: pos = pos + 1 while pos < len(mylist): #now pop and compare the two half lists print('mylist[', pos, ']=', mylist[pos], '; stack.top() =', stack.top()) if mylist[pos] != stack.pop(): return False pos = pos + 1 print('This is a palindrome') return True
def bracketChecker(input_string): open_brackets = ['(', '[', '{'] close_brackets = [')', ']', '}'] stack = Stack() for char in input_string: if char in open_brackets: stack.push(char) elif char in close_brackets: if char == ')': if stack.top() != '(': return False stack.pop() elif char == ']': if stack.top() != '[': return False stack.pop() elif char == '}': if stack.top() != '{': return False stack.pop() return True