本文整理汇总了Python中Stack类的典型用法代码示例。如果您正苦于以下问题:Python Stack类的具体用法?Python Stack怎么用?Python Stack使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Stack类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dectobin
def dectobin(n):
s=Stack()
while (n>0):
m=n%2
s.push(m)
n=int(n/2)
return s
示例2: convertStack
def convertStack(self,stack,newBase,oldBase):
revStack = Stack()
while stack.length() != 0:
revStack.push(stack.pop())
while revStack.length() != 0:
stack.push(Operation.__convertFromDecimal(
Operation.__convertToDecimal(revStack.pop(),oldBase),newBase))
del revStack
示例3: revrse
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)
示例4: __init__
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)
示例5: Queue_from_Stacks
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)
示例6: buildParseTree
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
示例7: postEval
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: buildParseTree
def buildParseTree(fpexp):
fplist = fpexp.split()#將str以sep分割成子字串,回傳儲存子字串的串列
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 == ')':
currentTree = pStack.pop()
else:
raise ValueError
return eTree
示例9: to_binary_num
def to_binary_num(decimal):
s = Stack()
while decimal > 0:
rem = decimal % 2
s.push(rem)
decimal /= 2
ans_string = ""
while not s.is_empty():
ans_string += str(s.pop())
return ans_string
示例10: dec_to_base
def dec_to_base(dec, base):
digits = "0123456789ABCDEF"
s = Stack()
while dec > 0:
s.push(dec % base)
dec = dec / base
yeni = ""
while not s.isEmpty():
yeni = yeni + digits[s.pop()]
return yeni
示例11: decTo
def decTo(self):
digits = "0123456789ABCDEF"
remstack = Stack()
while self.val > 0:
rem = self.val % self.base
remstack.push(rem)
self.val = self.val / self.base
newOne = ""
while not remstack.isEmpty():
newOne = newOne + digits[remstack.pop()]
return newOne
示例12: bracketChecker
def bracketChecker(user_input):
s = Stack()
for c in user_input:
if c == ("(" or "[" or "{"):
s.push(c)
elif c == (")" or "]" or "}"):
if s.isEmpty():
return False
else:
top = s.pop()
if not matches(top, c):
return False
else:
pass
return True
示例13: __init__
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()
示例14: balanced
def balanced(expr):
stck=Stack()
for each in expr:
if typeOfPara(each) is "lpar1" or typeOfPara(each) is "lpar2" or typeOfPara(each) is "lpar3":
typ=typeOfPara(each)
stck.push(typ)
elif typeOfPara(each) is "rpar1" or typeOfPara(each) is "rpar2" or typeOfPara(each) is "rpar3":
typ=typeOfPara(each)
spop=stck.pop()
if match(spop)==typ:
continue
else:
break
if stck.isEmpty() is not True:
print "Unbalanced Paranthesis"
else:
print "Balanced Paranthesis"
示例15: run_loop
def run_loop(params):
external = params
print 'external: ', external
cort_neurons = [MLEF.MorrisLecarElectricField(0.2, p=0.2) for e in range(num_cort_neurons)]
#motor_units = ST.generate_linear_spectrum_motor_units(10, 20e-04, 10e-03, 1.0, 11.0, 5.0, 10.0, 7.0, 14.0)
motor_units = ST.generate_linear_spectrum_motor_units(10, 20e-04, 200e-03, 1.0, 11.0, 5.0, 50.0, 7.0, 70.0)
stack = ST.Stack(run_time, cort_neurons, motor_units, cortical_soma_input=lambda t: soma_current[t], cortical_ext_input=lambda t: external)
stack.run()
stack.muscle.get_total_force()
return [e.total_force for e in stack.motor_units]