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


Python Stack.pop方法代码示例

本文整理汇总了Python中pythonds.basic.stack.Stack.pop方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.pop方法的具体用法?Python Stack.pop怎么用?Python Stack.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pythonds.basic.stack.Stack的用法示例。


在下文中一共展示了Stack.pop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: infixToPostfix

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def infixToPostfix(infixexpr):
   prec = {}
   prec["^"] = 4
   prec["*"] = 3
   prec["/"] = 3
   prec["+"] = 2
   prec["-"] = 2
   prec["("] = 1
   opStack = Stack()
   postfixList = []
   tokenList = infixexpr.split()

   for token in tokenList:
      if token in string.ascii_uppercase or token in string.digits:
         postfixList.append(token)
      elif token == "(":
         opStack.push(token)
      elif token == ")":
         topToken = opStack.pop()
         while topToken != "(":
            postfixList.append(topToken)
            topToken = opStack.pop()

      else:
         while (not opStack.isEmpty()) and (prec[opStack.peek()] >= prec[token]):
            postfixList.append(opStack.pop())
         opStack.push(token)

   while not opStack.isEmpty():
      postfixList.append(opStack.pop())
   return " ".join(postfixList)
开发者ID:hvu53,项目名称:algorithms1,代码行数:33,代码来源:2_stack_infix.py

示例2: infix_to_postfix

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
 def infix_to_postfix(self, infixexpr):
     opStack = Stack()
     postfixList = []
     tokenList = infixexpr.split()
     
     for token in tokenList:
         if BoolParser.isBoolVariable(token):
             # if token is a boolean variable, just append to list
             postfixList.append(token)
         elif token == '(':
             # start a new nested expression in the stack
             opStack.push(token)
         elif token == ')':
             # end the nested expression by moving the operators
             # from the stack to the list (i.e. in reverse order)
             topToken = opStack.pop()
             while topToken != '(':
                 postfixList.append(topToken)
                 topToken = opStack.pop()
         else:
             while (not opStack.isEmpty()) and \
                (BoolParser.PREC[opStack.peek()] >= BoolParser.PREC[token]):
                   postfixList.append(opStack.pop())
             opStack.push(token)
 
     while not opStack.isEmpty():
         postfixList.append(opStack.pop())
     
     return postfixList
开发者ID:eladnoor,项目名称:small-molecule-regulation,代码行数:31,代码来源:bool_parser.py

示例3: match_parenthesis

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def match_parenthesis(symbolString):
    ## close any and all open parenthesises & return a "file" to be processed further... 
    from pythonds.basic.stack import Stack
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol == "(":
            s.push(symbol+ str(index))
        elif symbol == ")":
            if s.isEmpty():
                balanced = False
            else:
                s.pop()

        index = index + 1

    if balanced and s.isEmpty():
        return symbolString
    elif balanced and not s.isEmpty():
        idx = int (s.pop().strip("("))
        return (match_parenthesis(removeExtra(symbolString,idx)))
    else:   #couldn't pop from stack
        return (match_parenthesis(removeExtra(symbolString,index-1)))
开发者ID:TScottJ,项目名称:external-tools,代码行数:27,代码来源:kifparser.py

示例4: infixToPostfix

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def infixToPostfix(infixexpr):
    prec = {}
    prec['*'] = 3
    prec['/'] = 3
    prec['+'] = 2
    prec['-'] = 2
    prec['('] = 1
    opStack = Stack()
    postfixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in 'ABCDEFGHIJKLMNOPQRSTUMWXYZ' or token in '0123456789':
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and (prec[opStack.peek()] >= prec[token]):
                postfixList.append(opStack.pop())
            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())

    return ' '.join(postfixList)
开发者ID:superdue,项目名称:code,代码行数:32,代码来源:infix_prefix_and_postfix_expressions.py

示例5: infixToPostfix

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def infixToPostfix(infixexpr):
    prec = {'*': 3, '/': 3, '+': 2, '-': 2, '(': 1, '^': 4}
    postfixList = []
    opStack = Stack()

    tokenList = infixexpr.split()
    for token in tokenList:
        if token in string.uppercase or token.isdigit():
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            top = opStack.pop()
            while top != '(':
                postfixList.append(top)
                top = opStack.pop()
        else:
            while not opStack.isEmpty() and prec[opStack.peek()] >= prec[token]:
                postfixList.append(opStack.pop())
            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())

    return " ".join(postfixList)
开发者ID:gr8h,项目名称:competitive_programming_py,代码行数:27,代码来源:revstring.py

示例6: buildParseTree

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def buildParseTree(fpexp):
    fplist = fpexp.split()
    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:ErikRHanson,项目名称:Problem-Solving-with-Algorithms-and-Data-Structures-Using-Python,代码行数:27,代码来源:parsetree.py

示例7: match_parenthesis

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def match_parenthesis (symbolString):
  ## close any and all open parenthesises & return a "file" to be processed further... 
  from pythonds.basic.stack import Stack
  s = Stack()
  balanced = True
  index = 0
  while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol == "(":
            s.push(symbol+ str(index))
        elif symbol == ")":
            if s.isEmpty():
                balanced = False
            else:
                s.pop()

        index = index + 1

  if balanced and s.isEmpty():
        #print ("it is FINALLY balanced!")
        return symbolString
  elif balanced and not s.isEmpty():
        #print "opening barace is not closed at " 
        
        idx = int (s.pop().strip("("))
        #print idx
        #print symbolString[idx]
        return (match_parenthesis(removeExtra(symbolString,idx))) 
  else:   #couldn't pop from stack
        #print "extra closing present at"
        #print index
        return (match_parenthesis(removeExtra(symbolString,index-1))) 
开发者ID:tanksha,项目名称:external-tools,代码行数:34,代码来源:sumo-importer.py

示例8: infixToPostfix

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def infixToPostfix(infixexpr):
	prec = {'^': 4, '*': 3, '/': 3, '+': 2, '-': 2, '(': 1}
	opStack = Stack()
	postfixList = []
	tokenList = infixexpr.split()

	for token in tokenList:
		if token in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or token in '1234567890':
			postfixList.append(token)

		elif token == '(':
			opStack.push('(')
		elif token == ')':
			topToken = opStack.pop()
			while topToken != '(':
				postfixList.append(topToken)
				topToken = opStack.pop()
		else:
			while (not opStack.isEmpty() ) and (prec[opStack.peek()] >= prec[token]):
				postfixList.append(opStack.pop())
			opStack.push(token)

	while not opStack.isEmpty():
		postfixList.append(opStack.pop())
	return ''.join(postfixList)
开发者ID:yuelingjiang,项目名称:workspace,代码行数:27,代码来源:algorithm2.py

示例9: infixToPostfix

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def infixToPostfix(infixexpr):
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    opStack = Stack()
    postfixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and \
               (prec[opStack.peek()] >= prec[token]):
                  postfixList.append(opStack.pop())
            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())
    return " ".join(postfixList)
开发者ID:teddymcw,项目名称:pythonds_algos,代码行数:32,代码来源:stacks.py

示例10: buildParseTree

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def buildParseTree(fpexp):
    fplist = fpexp.split()
    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:arunkamaraj,项目名称:learning,代码行数:27,代码来源:tree_trial.py

示例11: sortstacks

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def sortstacks(astack):
     temp=Stack()
     while not astack.isEmpty():
          tmp=astack.pop()
          while temp.peek()<tmp:
               temp.pop()
          temp.push(tmp)
     return temp
开发者ID:ssubramanian90,项目名称:Cracking-the-tech-interview,代码行数:10,代码来源:3-6.py

示例12: Conversion

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
class Conversion (object) :
    
    def __init__(self):
        self.prec = {}
        self.init_prec()
        self.op_stack = Stack() #operators will be kept in this stack
        self.output_list = []   #any postfix or prefix expression will be written in the list
        self.token_list = [] #initial expression will be read and kept in this list    
        self.operands = []
        self.init_operand()
        
    def init_prec(self) :
        self.prec["*"] = 3
        self.prec["/"] = 3
        self.prec["+"] = 2
        self.prec["-"] = 2
        self.prec["("] = 1
    
    def init_operand(self) :
        self.operands = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"
    
    def re_init(self) :
        #After any operation, the stack should be empty
        self.op_stack = Stack()
        self.output_list = []
        self.token_list = []

    def infixToPostfix(self,infixexpr) :
        '''The function will read an infix expression and write its 
        corresponding expression in the output_list
        doesn't return anything but alters the output_list'''
        self.re_init()
        self.token_list = infixexpr.split()
        for token in self.token_list :
            if token in self.operands :
                self.output_list.append(token)
            elif token == '(' :
                self.op_stack.push(token)
            elif token == ')' :
                top_token = self.op_stack.pop()
                while top_token != '(' :
                    self.output_list.append(top_token)
                    top_token = self.op_stack.pop()
            else :
                while (not self.op_stack.isEmpty()) and \
                    (self.prec[self.op_stack.peek()] >= self.prec[token]) :
                    self.output_list.append(self.op_stack.pop())
                self.op_stack.push(token)    
        while not self.op_stack.isEmpty() :
            self.output_list.append(self.op_stack.pop())             
    
    def getOutput(self) :
        return " ".join(self.output_list)

    def infixToPrefix(self, infixexpr) :
        pass
开发者ID:samjia533332,项目名称:Python_learning,代码行数:58,代码来源:infix_to_post.py

示例13: postfixEval

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
 def postfixEval(self, tokens):
     operandStack = Stack()
     for i in tokens:
         if i.isdigit():
             operandStack.push(int(i))
         else:
             operand2 = operandStack.pop()
             operand1 = operandStack.pop()
             result = self.compute(operand1, operand2, i)
             operandStack.push(result)
     return operandStack.pop()
开发者ID:haoxiang47,项目名称:Mars,代码行数:13,代码来源:Parser.py

示例14: postfixEval

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def postfixEval(postfixExpr):
	operandStack = Stack()
	tokenList = postfixExpr.split()

	for token in tokenList:
		if token in '0123456789':
			operandStack.push(int(token))
		elif token in '*/+-':
			num2 = operandStack.pop()
			num1 = operandStack.pop()
			operandStack.push(doMath(num1, num2, token))
	return operandStack.pop()
开发者ID:yuelingjiang,项目名称:workspace,代码行数:14,代码来源:algorithm2.py

示例15: evalPostfix

# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import pop [as 别名]
def evalPostfix(postfixExpr):
    operandStack = Stack()
    tokenList = postfixExpr.split()
    prec = {'*': 3, '/': 3, '+': 2, '-': 2, '(': 1}
    for token in tokenList:
        if token.isdigit():
            operandStack.push(int(token))
        elif token in prec:
            op2 = operandStack.pop()
            op1 = operandStack.pop()
            res = doMath(token, op1, op2)
            operandStack.push(res)
    return operandStack.pop()
开发者ID:gr8h,项目名称:competitive_programming_py,代码行数:15,代码来源:revstring.py


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