當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。