本文整理汇总了Python中Stack.size方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.size方法的具体用法?Python Stack.size怎么用?Python Stack.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.size方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: buildParseTree
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import size [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: __init__
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import size [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
示例3: NewStack
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import size [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()
示例4: infix2postfix
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import size [as 别名]
def infix2postfix(expr):
stck=Stack()
pof_expr=[]
for each in expr:
x=typeof(each)
if x is "lpar":
stck.push(each)
elif x is "rpar":
next_item=stck.pop()
while typeof(next_item) is not "lpar":
pof_expr.append(next_item)
next_item=stck.pop()
elif x is "oprand":
pof_expr.append(each)
elif x is "empty":
continue
elif x is "oprator":
while stck.size() is not 0 and precedence(each)<=precedence(stck.peek()):
pof_expr.append(stck.pop())
stck.push(each)
while stck.size()>0:
pof_expr.append(stck.pop())
print ''.join(pof_expr)
示例5: parChecker
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import size [as 别名]
def parChecker(symbolString):
s = Stack()
balanced = True
index =0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol in "([{":
s.push(symbol)
else:
if s.isEmpty():
balanced=False
else:
top = s.pop()
if not matches(top,symbol):
balanced=False
index=index+1
if balanced and s.isEmpty():
return True
else:
return s.size()
示例6: MyQueue
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import size [as 别名]
class MyQueue(object):
def __init__(self):
self.firstStack = Stack()
self.secondStack = Stack()
def enqueue(self, item):
self.firstStack.push(item)
def dequeue(self):
if self.secondStack.size() == 0:
while self.firstStack.size() > 0:
self.secondStack.push(self.firstStack.pop())
return self.secondStack.pop()
def peek(self):
if self.secondStack.size() == 0:
while self.firstStack.size() > 0:
self.secondStack.push(self.firstStack.pop())
return self.secondStack.peek()
def isEmpty(self):
return self.firstStack.size() == 0 and self.secondStack.size() == 0
示例7: print
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import size [as 别名]
print("------------Starting Stack Testing------------")
#Stack
from Stack import *
s = Stack()
#Operations #Stack Contents #Return Value
print(s.is_empty()) #[] True
s.push(4) #[4]
s.push('dog') #[4,'dog']
print(s.top()) #[4,'dog'] 'dog'
print(s.push(True)) #[4,'dog',True]
print(s.size()) #[4,'dog',True] 3
print(s.is_empty()) #[4,'dog',True] False
print(s.push(8.4)) #[4,'dog',True,8.4]
print(s.pop()) #[4,'dog',True] 8.4
print(s.pop()) #[4,'dog'] True
print(s.size()) #[4,'dog'] 2
print("------------Starting Queue Testing------------")
#Queue
from Queue import *
q = Queue()
#Operations #Queue Contents #Return Value
示例8: Stack
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import size [as 别名]
import Stack as Stack
s = Stack()
print(s.isEmpty())
s.push(4)
s.push("dog")
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())