本文整理汇总了Python中Stack.pop方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.pop方法的具体用法?Python Stack.pop怎么用?Python Stack.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.pop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: buildParseTree
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def buildParseTree(exp_string):
exp_list = exp_string.split()
parent_stack = Stack()
root = BinaryTree(None)
current_node = root
for token in exp_list:
if token == "(":
current_node.insertLeft(None)
parent_stack.push(current_node)
current_node = current_node.getLeftChild()
elif token in "+-*/":
current_node.setRootVal(token)
parent_stack.push(current_node)
current_node.insertRight(None)
current_node = current_node.getRightChild()
elif token == ")":
if parent_stack.size() > 0:
parent_node = parent_stack.pop()
current_node = parent_node
else:
current_node.setRootVal(float(token))
if parent_stack.size() > 0:
parent_node = parent_stack.pop()
current_node = parent_node
return root
示例2: postOrderTravel
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def postOrderTravel(self):
"""
Travel tree by post-order sequence
"""
newTree = self.clone()
if not newTree:
return
stack= Stack()
last = None
stack.push(newTree)
while stack.length():
item = stack.top()
if not item.getLeftChild() and not item.getRightChild() \
or last == item.getLeftChild() and not item.getRightChild() \
or last == item.getRightChild():
# visit the node who is either leaf node
# or node without right child and the left child has been visited
# or node whose right child is the last one been visited
print stack.pop().getValue()
last = item
else:
# push right child into stack first
if item.getRightChild():
stack.push(item.getRightChild())
# push left child into stack second
if item.getLeftChild():
stack.push(item.getLeftChild())
示例3: buildParseTree
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def buildParseTree(fpexp):
fplist = fpexp.split()
pStack = Stack()
eTree = BinaryTree('')
pStack.push(eTree)
currentTree = eTree
for i in fplist:
if i == '(':
currentTree.insertLeft('')
pStack.push(currentTree)
currentTree = currentTree.getLeftChild()
elif i not in ['+','-','*','/',')']:
currentTree.setRootVal(int(i))
parent = pStack.pop()
currentTree = parent
elif i in ['+','-','*','/']:
currentTree.setRootVal(i)
currentTree.insertRight('')
pStack.push(currentTree)
currentTree = currentTree.getRightChild()
elif i in ['+','-','*','/']:
currentTree.setRootVal(i)
currentTree.insertRight('')
pStack.push(currentTree)
currentTree = currentTree.getRightChild()
elif i == ')':
currentTree = pStack.pop()
else:
raise ValueError
return eTree
示例4: Queue_from_Stacks
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
class Queue_from_Stacks(object):
def __init__(self):
self.in_stack = Stack()
self.out_stack = Stack()
self.length = 0
def is_empty(self):
return self.length <= 0
def enqueue(self, element):
self.in_stack.push(element)
self.length += 1
def dequeue(self):
if(self.out_stack.is_empty()):
while (not(self.in_stack.is_empty())):
self.out_stack.push(self.in_stack.pop())
if (not(self.out_stack.is_empty())):
self.length -= 1
return self.out_stack.pop()
def __repr__(self):
return "IN: " + str(self.in_stack) + "\nOUT: " + str(self.out_stack)
示例5: NewStack
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
class NewStack():
def __init__(self):
self.Stack = Stack()
self.minStack = Stack()
self.tempMin = None
def isEmpty(self):
return self.Stack.isEmpty()
def push(self, item):
if self.tempMin == None or item < self.tempMin:
self.tempMin = item
self.Stack.push(item)
self.minStack.push(self.tempMin)
def pop(self):
self.minStack.pop()
return self.Stack.pop()
def min(self):
return self.minStack.peek()
def peek(self):
return self.Stack.peek()
def size(self):
return self.Stack.size()
示例6: infix_to_postfix
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def infix_to_postfix(infix_expr):
token_list = infix_expr.split()
op_stack = Stack()
postfix_list = []
prec = {"*":3, "/":3, "+":2, "-":2, "(":1}
for token in token_list:
if token in "0123456789" or token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
postfix_list.append(token)
elif token == "(":
op_stack.push(token)
elif token == ")":
top_token = op_stack.pop()
while top_token != "(":
postfix_list.append(top_token)
top_token = op_stack.pop()
else:
while (not op_stack.is_empty()) and prec[op_stack.peek()] >= prec[token]:
postfix_list.append(op_stack.pop())
op_stack.push(token)
while (not op_stack.is_empty()):
postfix_list.append(op_stack.pop())
return " ".join(postfix_list)
#print infix_to_postfix("A + B + C + D")
#print infix_to_postfix("( A + B ) * ( C + D ) ")
#print infix_to_postfix("A + B * C + D")
示例7: postEval
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def postEval(symbolString):
"""Given a postfix expression, evaluate it"""
if symbolString == None:
return None
tokens = symbolString.split()
stack = Stack()
for token in tokens:
# Is the token an integer?
if token.isdigit():
stack.push(int(token))
# otherwise, it must be an operator
elif stack.__len__() <= 1:
print("Ill-formed expression")
return None
else:
arg1 = stack.pop()
arg2 = stack.pop()
# Evaluate operator expressions (handles exceptions)
val = applyOp(token, arg1, arg2)
stack.push(val)
if not stack.__len__() == 1:
print("Ill-formed expression")
return None
return stack.pop()
示例8: __init__
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
class MarbleClock:
def __init__(self,numMarbles):
self.resv = Queue()
self.oneMin = Stack()
self.fiveMin = Stack()
self.hour = Stack()
assert numMarbles >= 27
for i in range(numMarbles):
self.resv.enqueue(i)
def minute(self):
m = self.resv.dequeue()
self.oneMin.push(m)
if self.oneMin.size() == 5:
self.fiveMin.push(self.oneMin.pop())
for i in range(4):
d = self.oneMin.pop()
self.resv.enqueue(d)
if self.fiveMin.size() == 12:
f = self.fiveMin.pop()
self.hour.push(f)
for i in range(11):
h = self.fiveMin.pop()
self.resv.enqueue(h)
if self.hour.size() == 12:
for i in range(12):
hrs = self.hour.pop()
self.resv.enqueue(hrs)
self.cap = self.resv
def run12hours(self):
for i in range(720): #720 because there are 720 minutes in 12 hours
self.minute()
def inOrder(self):
if self.cap == self.resv:
return True
#----------------------------------------------------------------------
def _getResv(self): #(Python) list of ints
nums = []
for i in range(self.resv.size()):
nums.append(self.resv._front.item)
self.resv.enqueue(self.resv.dequeue())
return nums
示例9: donusum
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def donusum(String):
s=Stack()
a=[]
j=0
sonuc=0
for i in String:
a=a+[i]
if a[0]== "0":
if a[1]=="b":
print "ikilik taban"
for i in a[2:]:
x=int(i)
s.push(x)
while not s.isEmpty():
b=s.pop()
sonuc=sonuc+b*math.pow(2,j)
j+=1
elif a[1]=="x":
print "onaltilik taban"
for i in a[2:]:
s.push(i)
while not s.isEmpty():
b=s.pop()
if b=="A":
sonuc=sonuc+10*math.pow(16,j)
elif b=="B":
sonuc=sonuc+11*math.pow(16,j)
elif b=="C":
sonuc=sonuc+12*math.pow(16,j)
elif b=="D":
sonuc=sonuc+13*math.pow(16,j)
elif b=="E":
sonuc=sonuc+14*math.pow(16,j)
elif b=="F":
sonuc=sonuc+15*math.pow(16,j)
else:
x=int(b)
sonuc=sonuc+x*math.pow(16,j)
j+=1
else:
print"sekizlik taban"
for i in a[1:]:
x=int(i)
s.push(x)
while not s.isEmpty():
b=s.pop()
sonuc=sonuc+b*math.pow(8,j)
j+=1
elif a[0] !="0":
print "onluk taban"
sonuc =int(String)
print "sonuc =", sonuc
示例10: postfix_eval
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def postfix_eval(postfix_expr):
token_list = postfix_expr.split()
operand_stack = Stack()
for token in token_list:
if token in "0123456789":
operand_stack.push(int(token))
else:
operand1 = operand_stack.pop()
operand2 = operand_stack.pop()
result = do_math(operand1, operand2, token)
operand_stack.push(result)
return operand_stack.pop()
示例11: __init__
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
class MaxStack:
def __init__(self):
self.base = Stack()
self.max = Stack()
def push(self, item):
if item >= self.max.peek():
self.max.push(item)
self.base.push(item)
def pop(self):
val = self.base.pop()
if (val == self.max.peek()):
self.max.pop()
return val
def getmax(self):
return self.max.peek()
示例12: parChecker
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def parChecker(symbolString):
s=Stack()
balanced = True # default
index =0
while index < len(symbolString) and balanced:
symbol=symbolString[index]
# if (,{,[ push onto stack
if symbol in "({[":
s.push(symbol)
else:
# if stack is empty, imbalance
if s.isEmpty():
balanced = False
else:
top=s.pop()
if not matches(top,symbol):
balanced = False
index = index+1
# balanced and no ( present
if balanced and s.isEmpty():
return True
# otherwise unbalanced ( present
else:
return False
示例13: postOrderTravel2
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def postOrderTravel2(self):
"""
second method of traveling tree by post-order sequence
"""
newTree = self.clone()
if not newTree:
return
stack = Stack()
# push tree into stack twice
stack.push(newTree)
stack.push(newTree)
while stack.length():
# pop out the first node
item = stack.pop()
# first pop pushes node's children into stack
# second pop visits the node
#
# if item equals the top item of stack means that item's children have not been visited yet
# then push them into stack
if stack.length() and item == stack.top():
if item.getRightChild():
stack.push(item.getRightChild())
stack.push(item.getRightChild())
if item.getLeftChild():
stack.push(item.getLeftChild())
stack.push(item.getLeftChild())
else:
print item.getValue()
示例14: __fromDecimal
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def __fromDecimal(self, base):
number = str(self.getDeciValue())
negative = True if "-" in number < 0 else False
if negative:
number = number[1:]
remainders = Stack()
separated_num = self.__separateFloat(str(float(number)))
integer = separated_num[0]
fraction = separated_num[1]
number = int(integer)
while number > 0:
remainder = number % base
remainders.push(int(remainder))
number = number // base
integer = "" if not remainders.is_empty() else "0"
while not remainders.is_empty():
try:
integer += Number.__DIGITS[remainders.pop()]
except IndexError as BaseError:
raise Exception( "Base outside range of representable digits.") from BaseError
fraction = "0." + fraction
fraction = self.__convertDecimalFraction(fraction, base)
output = integer + fraction
if negative:
output = "-" + output
self.__setValue(output)
示例15: revrse
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import pop [as 别名]
def revrse(expr):
revsd=[]
stck=Stack()
for each in expr:
stck.push(each)
while stck.isEmpty() is not True:
revsd.append(stck.pop())
print ''.join(revsd)