def reverseString(stack, string): for character in string: stack.push(character) reversedString = "" while not stack.isEmpty(): reversedString += stack.pop() return reversedString
def calculator(element, numbers): """ Calculator function that takes the first two numbers on the top of the stack and decides what type is the operand. """ a = int(stack.pop(numbers)) if not stack.isEmpty(numbers) else error() b = int(stack.pop(numbers)) if not stack.isEmpty(numbers) else error() #Checks operator's type if element == '+': result = plus(a, b) stack.push(numbers, result) elif element == '-': result = minus(a, b) stack.push(numbers, result) elif element == '*': result = multipl(a, b) stack.push(numbers, result) elif element == '/': result = division(a, b) stack.push(numbers, result)
def calculator(element, numbers): """ Calculator function that takes the first two numbers on the top of the stack and decides what type is the operand. """ a = int(stack.pop(numbers)) if not stack.isEmpty(numbers) else error() b = int(stack.pop(numbers)) if not stack.isEmpty(numbers) else error() #Checks operator's type if element == '+': result = plus(a,b) stack.push(numbers, result) elif element == '-': result = minus(a,b) stack.push(numbers, result) elif element == '*': result = multipl(a,b) stack.push(numbers, result) elif element == '/': result = division(a,b) stack.push(numbers, result)
def conv2hex(decD, dec_string): """ This will take in a decimal value in integer form and convert and return its hexadecimal value. """ value = dec_string new_stack = stack.getStack() while (value % 16) != 0: stack.push(new_stack, decD[value % 16]) value //= 16 final = '' while stack.isEmpty(new_stack) == False: final += str(stack.pop(new_stack)) return final
def main(): import stack mystack = stack.getStack() for item in range(1, 5): stack.push(mystack, item) print('Pushing', item, 'on stack') peek = stack.peek( mystack, (int(input('Enter an element you want to peek into the stack: ')))) if peek == True: print('Element is in stack') else: print('Element is not in stack') while not stack.isEmpty(mystack): item = stack.pop(mystack) print('Popping', item, 'from stack')
def dfs(maze, stack): length = len(maze) start = maze[0][0] goal = (9, 9) stack.push(start) while stack.isEmpty() == False: node = stack.pop() node.searched = True for x, y in (node.x + 1, node.y), (node.x - 1, node.y), (node.x, node.y - 1), (node.x, node.y + 1): if (0 <= x < length and 0 <= y < length) and ( maze[x][y].wall == False) and (maze[x][y].searched == False): if (x, y) == goal: return True else: maze[x][y].parent = node stack.push(maze[x][y]) return False
o1 = stack.pop(s) if o1 == None or o2 == None: #stack underflow print('Evaluation error') sys.exit(1) result = o1 + o2 stack.push(s, result) else: o2 = stack.pop(s) o1 = stack.pop(s) if o1 == None or o2 == None: #stack underflow print('Evaluation error') sys.exit(1) result = o1 - o2 stack.push(s, result) #is an operand else: stack.push(s, c) #stack is not empty if not stack.isEmpty(s): print('Evaluation error') sys.exit(1) if float.is_integer( evaluation ): #if result is integer, convert to integer and print print('Value of expression:', int(evaluation)) else: #print with two digits after fractional point print('Value of expression:', format(evaluation, '.2f'))
num1 = stack.pop(mainStack) num2 = stack.pop(mainStack) # do the given operation and push it to mainStack if item == '+': stack.push(mainStack, num2 + num1) elif item == '-': stack.push(mainStack, num2 - num1) elif item == '*': stack.push(mainStack, num2 * num1) elif item == '/': stack.push(mainStack, num2 / num1) # when item is '=' elif item == '=': if stack.isEmpty(mainStack): # stack underflow terminate() num = stack.pop(mainStack) if not stack.isEmpty(mainStack): # stack not empty after "=" terminate() # print value of expression if float.is_integer(num): # when num is an integer(ex: 1.0) print('Value of expression: {:}'.format(int(num))) else: # when num is non-integer float(ex:5.5) print('Value of expression: {:.2f}'.format(num))
# Set Variables openingParenthesis = ['(', '{', '['] closingParenthesis = [')', '}', ']'] mainStack = stack.getStack() isNestedProperly = True # Iterate ans for x in ans: # If x is a opening parenthesis, push x to mainStack if x in openingParenthesis: stack.push(mainStack, x) # If x is a closing parenthesis, pop one character from mainStack and assign it to poppedItem. elif x in closingParenthesis: # if stack is empty so that there is no left-pair left, assign isNestedProperly to False if stack.isEmpty(mainStack): isNestedProperly = False # if stack is not empty, pop one item from mainStack and assign it to poppedItem else: poppedItem = stack.pop(mainStack) # If the poppedItem is not a pair of x, assign isNestedProperly to False if closingParenthesis.index(x) != openingParenthesis.index( poppedItem): isNestedProperly = False # if ans is nested properly, check if any unpaired opening(left) parenthesis left if isNestedProperly and mainStack == []: isNestedProperly = True # print the result if isNestedProperly:
import stack import sys stk = stack.getStack() lst = list(input('Enter parentheses and/or braces: ')) for k in range(len(lst)): if lst[k] == '(': stack.push(stk, lst[k]) elif lst[k] == '{': stack.push(stk, lst[k]) elif lst[k] == ')': if stack.isEmpty(stk): print('Not properly nested.') sys.exit() if stack.pop(stk) != '(': print('Not properly nested.') sys.exit() elif lst[k] == '}': if stack.isEmpty(stk): print('Not properly nested.') sys.exit() if stack.pop(stk) != '{': print('Not properly nested.') sys.exit() if stack.isEmpty(stk): print('Nested properly.') else: print('Not properly nested.')
import stack mystack = stack.getStack() for item in range(1, 5): stack.push(mystack, item) print('Pushing', item, 'on stack') while not stack.isEmpty(mystack): item = stack.pop(mystack) print('Popping', item, 'from stack')
import stack """ Ask the user to enter a series of braces and parentheses, then indicate if they are properly nested """ char = input('Enter parentheses and/or braces: ') sequence = stack.getStack() #for every element in the received string checks the type and pushes/pops in/from the stack for el in char: if el == '(' or el == '{': stack.push(sequence, el) elif not stack.isEmpty(sequence): if el == ')' and stack.top(sequence) == '(': stack.pop(sequence) elif el == '}' and stack.top(sequence) == '{': stack.pop(sequence) #Final check if the stack is empty #If it's empty the string is proper if stack.isEmpty(sequence): print('Nested properly.') else: print('Not properly nested.')
# import stack module import stack # user input pa = input("Enter parentheses and/or braces: ") # define pa_stack pa_stack = stack.getStack() for i in range(0, len(pa)): # when character is left parentheses or braces push to the stack if pa[i] == "(" or pa[i] == "{": stack.push(pa_stack, pa[i]) # when character is right parentheses or braces pop the stack # if the value of the stack for top is left parentheses or braces else: if stack.top(pa_stack) == "(" and pa[i] == ")": stack.pop(pa_stack) elif stack.top(pa_stack) == "{" and pa[i] == "}": stack.pop(pa_stack) # output "Nested properly." when stack is empty if stack.isEmpty(pa_stack): print("Nested properly.") else: print("Not properly nested.")
import stack #creae an object stack = stack.Stack() #isEmpty() print stack.isEmpty() #display() stack.display() #push() for n in range(0,101,10): stack.push(n) #display() stack.display() #pop() for i in range(5): print "popped element", stack.pop() stack.display() #peek() print "top element = ", stack.peek() stack.display() print "popped element ", stack.pop() print "top element = ", stack.peek() stack.display()
import queue, stack q = queue.Queue() print('Queue Test:') for i in range(100): q.push(i) while not q.isEmpty(): print(q.pop()) print('Stack Test') stack = stack.Stack() for i in range(100): stack.push(i) while not stack.isEmpty(): print(stack.pop())
num2 = 0 result = 0 equal_flag = False # push the number to the stack while state == "Progress" and i < len(RPN_list): if RPN_list[i] == '+' or RPN_list[i] == '*' or RPN_list[ i] == '-' or RPN_list[i] == '/' or RPN_list[i] == '=': if RPN_list[i] == '=': equal_flag = True if len(RPN_stack) == 1 and i == len(RPN_list) - 1: result = float(stack.pop(RPN_stack)) else: state = "Error" else: if stack.isEmpty(RPN_stack) or len(RPN_stack) == 1: state = "Stack Underflow" break num2 = float(stack.pop(RPN_stack)) num1 = float(stack.pop(RPN_stack)) # evaluating the result if RPN_list[i] == '+': stack.push(RPN_stack, format(num1 + num2, '.2f')) elif RPN_list[i] == '*': stack.push(RPN_stack, format(num1 * num2, '.2f')) elif RPN_list[i] == '/': stack.push(RPN_stack, format(num1 / num2, '.2f')) elif RPN_list[i] == '-': stack.push(RPN_stack, format(num1 - num2, '.2f'))