def test_pop(self): my_stack = Stack(5) my_stack.push(2) my_stack.push(3) x = my_stack.pop() self.assertEqual(x, 3, "should be 3") self.assertEqual(my_stack.top, 0, "should be 0")
def __init__(self, value): self._root = NodeTree() self._size = 0 self._nodeAux = NodeTree() self._size = self._size + 1 self._root.__set_value__(value) self._stack = Stack() self._stack.__push__(value, 0)
def iterativeQuickSort(l, left, right, det = False): """ :param l: the list to sort. :param left: the left-most position. :param right: the right-most position. :param det: determinism. :return: void. """ theStack = Stack() theStack.push(left) theStack.push(right) while not theStack.isEmpty(): right = theStack.pop() left = theStack.pop() if __debug__: print ("quickSortIter-step({},{})".format(left, right)) if right <= left: continue mid = partition(l, left, right, det) theStack.push(left) theStack.push(mid - 1) theStack.push(mid + 1) theStack.push(right)
def dfs(self, rootId): """ Execute a Depth-First Search (DFS) in the graph starting from the specified node. :param rootId: the root node ID (integer). :return: the DFS list of nodes. """ # if the root does not exists, return None if rootId not in self.nodes: return None # DFS nodes initialization dfs_nodes = [] # queue initialization s = Stack() s.push(rootId) explored = {rootId} # nodes already explored while not s.isEmpty(): # while there are nodes to explore ... node = s.pop() # get the node from the stack explored.add(node) # mark the node as explored # add all adjacent unexplored nodes to the stack for adj_node in self.getAdj(node): if adj_node not in explored: s.push(adj_node) dfs_nodes.append(node) return dfs_nodes
def reverse(string): myStack = Stack(len(string)) for i in string: myStack.push(i) result = "" while not myStack.isEmpty(): result += myStack.pop() return result
def dtob(decimal, base = 2): myStack = Stack() while decimal > 0: myStack.push(decimal % base) decimal //= base # 나머지 result = "" while not myStack.isEmpty(): result += str(myStack.pop()) return result
def parseParenthesis(string): balanced = 1 index = 0 myStack = Stack(len(string)) while (index < len(string)) and (balanced == 1): check = string[index] if check == "(": myStack.push(check) else: if myStack.isEmpty(): balanced = 0 else: myStack.pop() index += 1 if balanced == 1 and myStack.isEmpty(): return True else: return False
def is_valid(self, s): array_stack = Stack() str_list = list(s) for index, char in enumerate(str_list): if char == '(' or char == '{' or char == '[': array_stack.push(char) else: if array_stack.is_empty(): return False top_char = array_stack.pop() if char == ')' and top_char != '(': return False if char == ']' and top_char != '[': return False if char == '}' and top_char != '{': return False return array_stack.is_empty()
def test_empty(self): empty_stack = Stack(5) is_empty = empty_stack.stack_empty() self.assertEqual(is_empty, True, "should be true")
def test_push(self): my_stack = Stack(5) my_stack.push(2) my_stack.push(3) self.assertEqual(my_stack.stack_array, [2, 3, 0, 0, 0], "should be 2,3,0,0,0")
def producer(stack): for i in range(0, 6): data = 'Task' + str(i) print('Producer pushing:', data) stack.push(data) sleep(2) def consumer(label, stack): while True: print(label, 'stack.pop():', stack.pop()) print('Create shared stack') stack = Stack() print('Stack:', stack) print('Creating and starting consumer threads') consumer1 = Thread(target=consumer, args=('Consumer1', stack)) consumer2 = Thread(target=consumer, args=('Consumer2', stack)) consumer3 = Thread(target=consumer, args=('Consumer3', stack)) consumer1.start() consumer2.start() consumer3.start() print('Creating and starting producer thread') producer = Thread(target=producer, args=[stack]) producer.start()
def addTwoNumbers(l1, l2): l1_node = l1 stack1 = Stack() while l1_node: stack1.push(l1_node.val) l1_node = l1_node.next l2_node = l2 stack2 = Stack() while l2_node: stack2.push(l2_node.val) l2_node = l2_node.next result = ListNode(None) s = 0 while stack1.size() > 0 and stack2.size() > 0: num1 = stack1.pop() num2 = stack2.pop() node = ListNode(((num1 + num2 + s) % 10)) node.next = result.next result.next = node s = int((num1 + num2 + s) / 10) stack = stack1 if stack1.size() > 0 else stack2 while stack.size() > 0: num = stack.pop() node = ListNode(((num + s) % 10)) node.next = result.next result.next = node s = int((num + s) / 10) if s > 0: node = ListNode(s) node.next = result.next result.next = node return result.next
def infixToPostfix(myExp, myStack): postFix = [] for i in range(len(myExp)): if (isOperand(myExp[i])): postFix.append(myExp[i]) elif (myExp[i] == "("): myStack.push(myExp[i]) elif (myExp[i] == ")"): topOperator = myStack.pop() while (not myStack.isEmpty() and topOperator != "("): postFix.append(topOperator) topOperator = myStack.pop() else: while (not myStack.isEmpty()) and (precedence(myExp[i]) <= precedence((myStack.peek()))): postFix.append(myStack.pop()) myStack.push(myExp[i]) while (not myStack.isEmpty()): postFix.append(myStack.pop()) return " ".join(postFix) if __name__ == "__main__": myExp = 'a+b*(c^d-e)^(f+g*h)-i' myExp = [i for i in myExp] print("Infix: ", " ".join(myExp)) myStack = Stack(len(myExp)) print("Postfix: ", infixToPostfix(myExp, myStack))
for element in arrayUtility.array: print(str(element)) result = arrayUtility.search_max() print("The max element is " + str(result)) result = arrayUtility.search_min() print("The min element is " + str(result)) looking_for_element = 80 position_result = arrayUtility.init_binary_search(looking_for_element) print("Find the " + str(looking_for_element) + " in the position " + str(position_result)) print("-----Stack-----") stack = Stack() stack.push("2") stack.push("3") stack.push("1") print("TOP STACK " + str(stack.peak())) stack.reverse() print("TOP STACK " + str(stack.peak())) print("-----Normal Queue-----") queue = NormalQueue() queue.push("1") print("FRONT LINE" + str(queue.peak_first())) print("BACK LINE" + str(queue.peak_back())) queue.push("2") print("FRONT LINE" + str(queue.peak_first()))
class TreeBinary: def __init__(self, value): self._root = NodeTree() self._size = 0 self._nodeAux = NodeTree() self._size = self._size + 1 self._root.__set_value__(value) self._stack = Stack() self._stack.__push__(value, 0) def _is_child_left(self, root): if (root.__get_left__() != None): return True return False def _is_child_right(self, root): if (root.__get_right__() != None): return True return False def _child(self, root, value): if ((root.__get_value__() < value) and (self._is_child_left(root) == False)): return root if ((root.__get_value__() > value) and (self._is_child_right(root) == False)): return root if ((root.__get_value__() < value) and (root.__get_left__() != None)): return self._child(root.__get_left__(), value) if ((root.__get_value__() > value) and (root.__get_right__() != None)): return self._child(root.__get_right__(), value) return root def __is_empty_left(self): if (self._root.__get_left__() != None): return True return False def __is_empty_right(self): if (self._root.__get_right__() != None): return True return False def __root__(self): return self._root def __size__(self): return self._size def __insert__(self, value): self._size = self._size + 1 self._stack.__push__(value, 0) if ((self.__is_empty_left() == False) and (value < self._root.__get_value__())): node = NodeTree() node.__set_father__(self._root) node.__set_value__(value) self._root.__set_left__(node) elif ((self.__is_empty_right() == False) and (value > self._root.__get_value__())): node = NodeTree() node.__set_value__(value) node.__set_father__(self._root) self._root.__set_right__(node) else: if (value < self._root.__get_value__()): child = self._child(self._root.__get_left__(), value) if (child != None): if (value > child.__get_value__()): node = NodeTree() node.__set_value__(value) node.__set_father__(child) child.__set_right__(node) else: node = NodeTree() node.__set_value__(value) node.__set_father__(child) child.__set_left__(node) else: child = self._child(self._root.__get_right__(), value) if (child != None): if (value > child.__get_value__()): node = NodeTree() node.__set_value__(value) node.__set_father__(child) child.__set_right__(node) else: node = NodeTree() node.__set_value__(value) node.__set_father__(child) child.__set_left__(node) def __search_node__(self, root, value): if (root == self._root): if (value > root.__get_value__()): root = root.__get_right__() self._nodeAux = root elif (value < root.__get_value__()): root = root.__get_left__() self._nodeAux = root else: return root if (value == self._nodeAux.__get_value__()): return self._nodeAux else: if ((root.__get_left__() != None) and (root.__get_value__() != value)): self._nodeAux = root.__get_left__() self.__search_node__(root.__get_left__(), value) if ((root.__get_right__() != None) and (root.__get_value__() != value)): self._nodeAux = root.__get_right__() self.__search_node__(root.__get_right__(), value) return self._nodeAux def __is_leaf__(self, root): if (root.__get_left__() == None and root.__get_right__() == None): return True return False def __search_min__(self, root, value): search = SearchUtil(self._stack.__extract_values__()) if (root == self._root): node = self.__search_node__(root, value) if (self.__is_leaf__(node)): return None if (value > root.__get_value__()): root = root.__get_right__() self._nodeAux = root if (value < root.__get_value__()): root = root.__get_left__() self._nodeAux = root searchRet = search.search_for_previous_value(value) if (searchRet == self._nodeAux.__get_value__()): return self._nodeAux if (root.__get_left__() != None and searchRet != self._nodeAux.__get_value__()): self._nodeAux = root.__get_left__() self.__search_min__(root.__get_left__(), value) if (root.__get_right__() != None and searchRet != self._nodeAux.__get_value__()): self._nodeAux = root.__get_right__() self.__search_min__(root.__get_right__(), value) return self._nodeAux def __extract_values__(self): return self._stack.__extract_values__() def __remove__(self, root, value): self._size = self._size - 1 nodeTree = self.__search_node__(root, value) if (nodeTree != None): nodeFather = nodeTree.__get_father__() if (nodeFather != None): self._stack.__pop_value__(value) if (self._is_child_left(nodeTree) and self._is_child_right(nodeTree)): nodeMin = self.__search_min__(root, value) nodeMin.__set_father__(nodeFather) if (nodeTree.__get_value__() > nodeMin.__get_value()): nodeMin.__set_right__(nodeTree.__get_right__()) nodeTree.__get_right__().__set_father(nodeMin) else: nodeTree.__get_left__().__set_father(nodeMin) nodeMin.__set_left__(nodeTree.__get_left__()) if (nodeFather.__get_value__() > nodeMin.__get_value__()): nodeFather.__set_left__(nodeMin) else: nodeFather.__set_right__(nodeMin) else: if (self.__is_leaf__(nodeTree)): if (nodeTree.__get_value__() < nodeFather.__get_value__()): nodeFather.__set_left__(None) else: nodeFather.__set_right__(None) else: if (nodeTree.__get_value__() < nodeFather.__get_value__()): if (not self._is_child_left(nodeTree) and self._is_child_right(nodeTree)): nodeTree.__get_right__().__set_father__( nodeFather) nodeFather.__set_left__( nodeTree.__get_right__()) else: nodeTree.__get_left__().__set_father__( nodeFather) nodeFather.__set_left__( nodeTree.__get_left__()) else: if (not self._is_child_left(nodeTree) and self._is_child_right(nodeTree)): nodeTree.__get_right__().__set_father__( nodeFather) nodeFather.__set_right__( nodeTree.__get_right__()) else: nodeTree.__get_right__().__set_father__( nodeFather) nodeFather.__set_right__( nodeTree.__get_left__()) else: if (nodeTree == self._root): if (self._is_child_left(nodeTree) and self._is_child_right(nodeTree)): nodeMin = self.__search_min__(self._root, value) nodeFatherRemove = nodeMin.__get_father__() if (nodeFatherRemove.__get_value__() > nodeMin.__get_value__()): if (not self._is_child_left(nodeMin) and not self._is_child_right(nodeMin)): nodeFatherRemove.__set_left__(None) else: if (self._is_child_left(nodeMin) and not self._is_child_right(nodeMin)): nodeFatherRemove.__set_left__( nodeMin.__get_left__()) else: if (not self._is_child_left(nodeMin) and self._is_child_right(nodeMin)): nodeFatherRemove.__set_left__( nodeMin.__get_right__()) else: if (not self._is_child_left(nodeMin) and not self._is_child_right(nodeMin)): nodeFatherRemove.__set_right__(None) else: if (self._is_child_left(nodeMin) and not self._is_child_right(nodeMin)): nodeFatherRemove.__set_right__( nodeMin.__get_left__()) else: if (not self._is_child_left(nodeMin) and self._is_child_right(nodeMin)): nodeFatherRemove.__set_right__( nodeMin.__get_right__()) nodeMin.__set_left__(self._root.__get_left__()) nodeMin.__set_right__(self._root.__get_right__()) nodeMin.__set_father__(None) self._root = nodeMin else: if (not self._is_child_left(nodeTree) and not self._is_child_right(nodeTree)): nodeTree = None self._root = nodeTree else: if (self._is_child_left(nodeTree) and not self._is_child_right(nodeTree)): child = nodeTree.__get_left__() child.__set_left__(None) child.__set_right__(None) child.__set_father__(None) self._root = child else: if (not self._is_child_left(nodeTree) and self._is_child_right(nodeTree)): child = nodeTree.__get_right__() child.__set_left__(None) child.__set_right__(None) child.__set_father__(None) self._root = child
def check_bal_par(par_str): if not len(par_str): return True if len(par_str) % 2 != 0: return False s = Stack() for item in par_str: if s.is_empty(): s.push(item) else: if s.peek() == '(' and item == ')': s.pop() else: s.push(item) return s.is_empty()
from stack.Stack import Stack __author__ = 'lijiayan' if __name__ == '__main__': s = Stack() s.push(1) s.push(2) s.push(3) for i in range(s.size()): print(s.pop()) print("栈大小:" + str(s.size()))
from stack.Stack import Stack s = Stack() print(s.get_size()) print(s.is_empty()) print(s.peek()) s.print_stack() s.push(5) s.push(2) s.push(28) s.push(11) s.push(18) print(s.get_size()) print(s.is_empty()) print(s.peek()) s.print_stack() print(s.pop()) print(s.pop()) print(s.pop()) print(s.pop()) print(s.pop()) s.print_stack()