本文整理汇总了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)
示例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
示例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)))
示例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)
示例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)
示例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)))
示例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)
示例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)
示例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
示例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
示例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
示例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()
示例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()
示例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()