def dfs_traversal_helper(self, source, visited): # Initiate Result String for this traversal result = "" # Initiate Stack for this traversal stack = Stack() # Push source onto Stack stack.push(source) # Updated process array to say it has been visited/placed in stack for future processing visited[source] = True # Processing the stack while not stack.is_empty(): # Remove from stack curr_data = stack.pop() # Do what you are supposed to do with the node result += str(curr_data) # Enter its list of adjacent nodes, starting with the first one temp = self.array[curr_data].head # Add all adjacent nodes to the stack while temp is not None: # If this node has not been visited or placed in stack already if not visited[temp.data]: # Push onto the stack stack.push(temp.data) # Mark as visited for future processing visited[temp.data] = True # Add the next node as well temp = temp.next # Once entire stack is processed, return the resulting output and the visited array return result, visited
def has_balanced_parantheses(expression): stack = Stack() if expression: for char in expression: print(char) if char == "(": stack.push(char) elif char == ")": if stack.pop() == None: return False if len(stack) == 0: return True return False
def run(): #Stacks.problem_balanced_parantheses.run() stack = Stack() for i in range(10): stack.push(i + 1) for i in range(10): stack.pop() print(stack.minimum())
def rev_string(string): s = Stack() for character in string: s.push(character) l = [] while not s.is_empty(): l.append(s.pop()) return ''.join(l)
def dfs_traversal_helper(self, source, visited): result = "" stack = Stack() stack.push(source) visited[source] = True while not stack.is_empty(): r = stack.pop() result += str(r) for index in range(len(self.adjMatrix[r])): if index not in visited and self.adjMatrix[r][index] == 1: stack.push(index) visited[index] = True return result, visited
def build_parse_tree(fpexp): fp_list = fpexp.split() p_stack = Stack() e_tree = BinaryTree('') p_stack.push(e_tree) current_tree = e_tree for i in fp_list: if i == '(': current_tree.set_left_child('') p_stack.push(current_tree) current_tree = current_tree.get_left_child() elif i not in '+-*/)': current_tree.set_root_val(eval(i)) parent = p_stack.pop() current_tree = parent elif i in '+0*/': current_tree.set_root_val(i) current_tree.set_right_child('') p_stack.push(current_tree) elif i == ')': current_tree = p_stack.pop() else: raise ValueError("Unknown Operator: " + i) return e_tree
def infix2postfix(infixexpr): """ Transform a infix compression to its equivalent postfix compression infixexpr: A infix math compression consists of elementary arithmetic wherein each operator and operand should be split by SPACE. """ prec = {"*": 3, '/': 3, '+': 2, '-': 2, '(': 1} opStack = Stack() postfixList = [] tokenList = infixexpr.split() for token in tokenList: if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789": postfixList.append(token) elif token == '(': opStack.push(token) elif token == ')': topToken = opStack.pop() while topToken != '(': postfixList.append(topToken) topToken = opStack.pop() else: while (not opStack.is_empty()) and \ (prec[opStack.peek()] >= prec[token]): postfixList.append(opStack.pop()) opStack.push(token) while not opStack.is_empty(): postfixList.append(opStack.pop()) return " ".join(postfixList)
def run(): expression = "3 1 + 4 *" operands = Stack(); for char in expression.split(' '): if char == "*": op2 = operands.pop() op1 = operands.pop() output = int(op1) * int(op2) operands.push(output) elif char == "/": op2 = operands.pop() op1 = operands.pop() output = int(op1) / int(op2) operands.push(output) elif char == "+": op2 = operands.pop() op1 = operands.pop() output = int(op1) + int(op2) operands.push(output) elif char == "-": op2 = operands.pop() op1 = operands.pop() output = int(op1) - int(op2) operands.push(output) else: operands.push(char) print(operands.pop())