class Queue(): def __init__(self): self.s1 = Stack() self.s2 = Stack() def Push(self, i): self.s1.Push(i) def Pop(self): while self.s1.IsEmpty() == False: self.s2.Push(self.s1.Pop()) outdat = self.s2.Pop() while self.s2.IsEmpty() == False: self.s1.Push(self.s2.Pop()) return outdat def Peek(self): while self.s1.IsEmpty() == False: self.s2.Push(self.s1.Pop()) outdat = self.s2.Peek() while self.s2.IsEmpty() == False: self.s1.Push(self.s2.Pop()) return outdat def IsEmpty(self): return self.s1.IsEmpty() def __repr__(self): return str(self.s1)[::-1]
class Queue: def __init__(self): self.length = 0 self.s1 = Stack() self.s2 = Stack() def Insert(self, elem): self.s1.Push(elem) self.length += 1 def Remove(self): if self.s2.Is_Empty(): #switch the entire stack over s2 while not self.s1.Is_Empty(): node = self.s1.Pop() self.s2.Push(node) elem = self.s2.Pop() self.length -= 1 return str(elem) def Is_Empty(self): return self.length == 0
def SortStack(s): temp = Stack() while s.IsEmpty() == False: if temp.IsEmpty(): temp.Push(s.Pop()) if s.Peek() > temp.Peek(): hold = s.Pop() s.Push(temp.Pop()) s.Push(hold) elif s.Peek() <= temp.Peek(): temp.Push(s.Pop()) return temp
from stack import Stack expression = "((A+(B*C))-D)" # expression="(A+B*(C-D))" results = "" mystack = Stack() for i in range(len(expression)): if expression[i] >= "A" and expression[i] <= "Z": results += expression[i] else: mystack.Push(expression[i]) if expression[i] == ")": mystack.Pop() # pop ")" while mystack.stack[mystack.top] != "(": results += mystack.Pop() mystack.Pop() # pop "(" print("postfix of expression\"{}\" is: {}".format(expression, results))
from stack import Stack mystack = Stack() mystack.stack = ['A', 'C', 'B', 'A', 'D', 'F', 0, 0, 0, 0] mystack.top = 5 mystack.PrintStack() tempstack = Stack() for i in range(mystack.top + 1): if mystack.stack[mystack.top] != "A": tempstack.Push(mystack.Pop()) else: mystack.Pop() for j in range(tempstack.top + 1): mystack.Push(tempstack.Pop()) tempstack.PrintStack() # should be empty mystack.PrintStack()
from stack import Stack # expression = "43-15*+" expression = "15+2-6*" mystack = Stack() for i in range(len(expression)): if expression[i] >= "0" and expression[i] <= "9": mystack.Push(expression[i]) if expression[i] == "+": mystack.Push(int(mystack.Pop()) + int(mystack.Pop())) elif expression[i] == "-": mystack.Push(-(int(mystack.Pop()) - int(mystack.Pop()))) elif expression[i] == "*": mystack.Push(int(mystack.Pop()) * int(mystack.Pop())) elif expression[i] == "/": mystack.Push(int(mystack.Pop()) // int(mystack.Pop())) print("\"{}\" = {}".format(expression, mystack.Pop()))
elif maze[x][y+1] == 0: # east y += 1 go = True elif maze[x+1][y] == 0: # south x += 1 go = True elif maze[x][y-1] == 0: # west y -= 1 go = True else: # dead end go = False print("Dead end...") if go: path_x.Push(x) path_y.Push(y) maze[x][y] = 2 else: path_x.Pop() path_y.Pop() x, y = path_x.stack[path_x.top], path_y.stack[path_y.top] print(">>> Maze solved! <<<") print(np.matrix(maze))