當前位置: 首頁>>代碼示例>>Python>>正文


Python Stack.push方法代碼示例

本文整理匯總了Python中Stack.push方法的典型用法代碼示例。如果您正苦於以下問題:Python Stack.push方法的具體用法?Python Stack.push怎麽用?Python Stack.push使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Stack的用法示例。


在下文中一共展示了Stack.push方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: parChecker

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [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
開發者ID:lbovard,項目名稱:python_stuff,代碼行數:28,代碼來源:par_check.py

示例2: dectobin

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [as 別名]
def dectobin(n):
    s=Stack()
    while (n>0):
        m=n%2
        s.push(m)
        n=int(n/2)
    return s
開發者ID:KartikPadmanabhan,項目名稱:AlgorithmDesign,代碼行數:9,代碼來源:dectobinary.py

示例3: __fromDecimal

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [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)
開發者ID:diarmaidob,項目名稱:BaseConv,代碼行數:28,代碼來源:BaseConvert.py

示例4: buildParseTree

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [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
開發者ID:Xiaoke1982,項目名稱:AlgorithmAndDataStructureInPython,代碼行數:32,代碼來源:ParseTree.py

示例5: NewStack

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [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()
開發者ID:Jason-Yuan,項目名稱:Interview-Code,代碼行數:29,代碼來源:Chapter3-2.py

示例6: postEval

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [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()
開發者ID:Taylor4484,項目名稱:CS-313E---Elements-of-Software-Design,代碼行數:30,代碼來源:Calculator.py

示例7: Queue_from_Stacks

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [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)
開發者ID:g2graman,項目名稱:Miscellaneous,代碼行數:29,代碼來源:Queue_from_Stacks.py

示例8: infix_to_postfix

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [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")
開發者ID:Xiaoke1982,項目名稱:AlgorithmAndDataStructureInPython,代碼行數:33,代碼來源:infix_to_postfix.py

示例9: convertStack

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [as 別名]
 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,代碼行數:10,代碼來源:Operation.py

示例10: testPop

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [as 別名]
 def testPop(self):
     stack = Stack()
     stack.push(0)
     stack.push(5)
     stack.push(2)
     item = stack.pop()
     self.assertEquals(item, 2)
     self.assertEquals(stack.data, [0, 5])
開發者ID:ChaseSnapshot,項目名稱:algorithms,代碼行數:10,代碼來源:StackTest.py

示例11: revrse

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [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)
開發者ID:sanchiaman,項目名稱:python_github,代碼行數:10,代碼來源:reverseTheString.py

示例12: SortStack

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [as 別名]
def SortStack(mystack):
	HelpStack = Stack()
	while not mystack.isEmpty():
		MaxValue = mystack.pop()
		while not HelpStack.isEmpty() and HelpStack.peek() > MaxValue:
			mystack.push(HelpStack.pop())
		HelpStack.push(MaxValue)

	return HelpStack
開發者ID:Jason-Yuan,項目名稱:Interview-Code,代碼行數:11,代碼來源:Chapter3-6.py

示例13: to_binary_num

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [as 別名]
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,代碼行數:13,代碼來源:toBinary.py

示例14: dec_to_base

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [as 別名]
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,代碼行數:13,代碼來源:dec_to_base.py

示例15: decTo

# 需要導入模塊: import Stack [as 別名]
# 或者: from Stack import push [as 別名]
	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,代碼行數:16,代碼來源:decTo.py


注:本文中的Stack.push方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。