当前位置: 首页>>代码示例>>Python>>正文


Python Stack类代码示例

本文整理汇总了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
开发者ID:KartikPadmanabhan,项目名称:AlgorithmDesign,代码行数:7,代码来源:dectobinary.py

示例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
开发者ID:WrathRockeu,项目名称:CS2513-L4,代码行数:8,代码来源:Operation.py

示例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)
开发者ID:sanchiaman,项目名称:python_github,代码行数:8,代码来源:reverseTheString.py

示例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)
开发者ID:paulchifita,项目名称:osafunsa,代码行数:10,代码来源:marbleClock.py

示例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)
开发者ID:g2graman,项目名称:Miscellaneous,代码行数:27,代码来源:Queue_from_Stacks.py

示例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
开发者ID:Xiaoke1982,项目名称:AlgorithmAndDataStructureInPython,代码行数:30,代码来源:ParseTree.py

示例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()
开发者ID:Taylor4484,项目名称:CS-313E---Elements-of-Software-Design,代码行数:28,代码来源:Calculator.py

示例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
开发者ID:Zoxas,项目名称:Test,代码行数:30,代码来源:Tree_Parse.py

示例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
开发者ID:Xiaoke1982,项目名称:AlgorithmAndDataStructureInPython,代码行数:11,代码来源:toBinary.py

示例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
开发者ID:mctr,项目名称:pro-lang,代码行数:11,代码来源:dec_to_base.py

示例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
开发者ID:wemrekurt,项目名称:Python,代码行数:14,代码来源:decTo.py

示例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
开发者ID:keachico,项目名称:CS303E_and_CS313E,代码行数:15,代码来源:stack_bracketCheck.py

示例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()
开发者ID:gautamgitspace,项目名称:CTCIv5.0,代码行数:15,代码来源:largest-stack.py

示例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"
开发者ID:sanchiaman,项目名称:python_github,代码行数:19,代码来源:balancedParanthesis.py

示例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]
开发者ID:rllin,项目名称:motor_unit_stack,代码行数:10,代码来源:electric_field_rfd.py


注:本文中的Stack类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。