def dfs_traversal_myCode(g, source): result = "" stack = myStack() ver = g.vertices if ver == 0: return result visited = [] for i in range(ver): visited.append(False) stack.push(source) while not stack.isEmpty(): current = stack.pop() visited[current] = True result += str(current) temp = g.array[current].head_node while temp is not None: stack.push(temp.data) temp = temp.next_element for i in range(ver): if visited[i] is False: stack.push(i) while not stack.isEmpty(): current = stack.pop() visited[current] = True result += str(current) temp = g.array[current].head_node while temp is not None: stack.push(temp.data) temp = temp.next_element return result
def enqueue(self, value): helper_stack=myStack() while not self.mainStack.isEmpty(): helper_stack.push(self.mainStack.pop()) self.mainStack.push(value) while not helper_stack.isEmpty(): self.mainStack.push(helper_stack.pop())
def evaluatePostFix_myCode(exp): stack = myStack() for i in exp: if i.isdigit(): stack.push(i) else: num1 = stack.pop() num2 = stack.pop() result = eval(num2 + i + num1) print(result) stack.push(str(result)) return stack.pop()
def evaluatePostFix(exp): stack = myStack() try: for char in exp: if char.isdigit(): # Push numbers in stack stack.push(char) else: # use top two numbers and evaluate right = stack.pop() left = stack.pop() stack.push(str(eval(left + char + right))) # final answer should be a number return int(float(stack.pop())) except TypeError: return "Invalid Sequence"
def reverseK(queue, k): if queue.isEmpty() is True or k > queue.size() or k < 0: # Handling invalid input return None stack = myStack() for i in range(k): stack.push(queue.dequeue()) while stack.isEmpty() is False: queue.enqueue(stack.pop()) size = queue.size() for i in range(size - k): queue.enqueue(queue.dequeue()) return queue
def isBalanced(exp): closing = ['}', ')', ']'] stack = myStack() for character in exp: if character in closing: if stack.isEmpty(): return False topElement = stack.pop() if (character == '}' and topElement != '{'): return False if (character == ')' and topElement != '('): return False if (character == ']' and topElement != '['): return False else: stack.push(character) if (stack.isEmpty() is False): return False return True
def reverseK_myCode(queue, k): q_size = queue.size() if k > q_size or k < 0 or k == 0: return None s = myStack() while k > 0: s.push(queue.dequeue()) k -= 1 while not s.isEmpty(): queue.enqueue(s.pop()) for i in range( q_size - k): # this code fails as k is down to 0 now and this for wont work queue.enqueue(queue.dequeue()) return queue
def sortStack(stack): tempStack = myStack() while stack.isEmpty() is False: value = stack.pop() if tempStack.top() is not None and value >= int(tempStack.top()): # if value is not none and larger, push it at the top of tempStack tempStack.push(value) else: while tempStack.isEmpty() is False: stack.push(tempStack.pop()) # place value as the smallest element in tempStack tempStack.push(value) # Transfer from tempStack => stack while tempStack.isEmpty() is False: stack.push(tempStack.pop()) return stack
def dfs_traversal_helper(g, source, visited): result = "" # Create Stack(Implemented in previous lesson) for Depth First Traversal # and Push source in it stack = myStack() stack.push(source) visited[source] = True # Traverse while stack is not empty while (stack.isEmpty() is False): # Pop a vertex/node from stack and add it to the result current_node = stack.pop() result += str(current_node) # Get adjacent vertices to the current_node from the array, # and if they are not already visited then push them in the stack temp = g.array[current_node].head_node while (temp is not None): if (visited[temp.data] is False): stack.push(temp.data) # Visit the node visited[temp.data] = True temp = temp.next_element return result, visited # For the above graph it should return "12453"
def nextGreaterElement(lst): s = myStack() res = [-1] * len(lst) # Reverse iterate list for i in range(len(lst) - 1, -1, -1): # if stack has elements: if not s.isEmpty(): # While stack has elements # and current element is greater than top element # pop all elements while not s.isEmpty() and s.top() <= lst[i]: s.pop() # if stack has an element # Top element will be greater than ith element if not s.isEmpty(): res[i] = s.top() # push in the current element in stack s.push(lst[i]) for i in range(len(lst)): print(str(lst[i]) + " -- " + str(res[i])) return res
else: while tempStack.isEmpty() is False: stack.push(tempStack.pop()) # place value as the smallest element in tempStack tempStack.push(value) # Transfer from tempStack => stack while tempStack.isEmpty() is False: stack.push(tempStack.pop()) return stack def sortStack_dont_recomment(stack): stack.stackList.sort(reverse=True) return stack stack = myStack() stack.push(2) stack.push(97) stack.push(4) stack.push(42) stack.push(12) stack.push(60) stack.push(23) sortStack(stack) # Printing by popping print([stack.pop() for i in range(stack.size())])
def __init__(self): # Can use size from argument to create stack self.mainStack = myStack() self.tempStack = myStack()
def __init__(self): self.mainStack = myStack()
def __init__(self): self.minStack = myStack() self.mainStack = myStack() return