本文整理汇总了Python中pythonds.basic.stack.Stack.peek方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.peek方法的具体用法?Python Stack.peek怎么用?Python Stack.peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pythonds.basic.stack.Stack
的用法示例。
在下文中一共展示了Stack.peek方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_num
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [as 别名]
def parse_num(list):
no_operator = re.split(r'[(+)notandor]', list)
no_operator = [i for i in no_operator if i != '']
no_operand = re.split(r'[0123456789]', list)
no_oper = []
parsed = []
p = Stack()
count = 0
for i in range(len(no_operand)):
if no_operand[i] == "(":
p.push(no_operand[i])
if no_operand[i] in ['+', '-', '*', '/', ')', 'not', 'and', 'or']:
if p.peek() != '':
p.push('')
p.push(no_operand[i])
else:
p.push(no_operand[i])
if no_operand[i] == '':
if p.peek() != '':
p.push('')
no_oper = p.items
for i in range(len(no_oper)):
if no_oper[i] != '':
parsed.append(no_oper[i])
else:
parsed.append(no_operator[count])
count += 1
return parsed
示例2: infixToPostfix
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [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)
示例3: infixToPostfix
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [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)
示例4: infix_to_postfix
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [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
示例5: infixToPostfix
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [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: infixToPostfix
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [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)
示例7: infixToPostfix
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [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)
示例8: sortstacks
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [as 别名]
def sortstacks(astack):
temp=Stack()
while not astack.isEmpty():
tmp=astack.pop()
while temp.peek()<tmp:
temp.pop()
temp.push(tmp)
return temp
示例9: Conversion
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [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
示例10: infix_to_post
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [as 别名]
def infix_to_post(str):
tokenList = str.split()
length = len(tokenList)
cnt_pa = 0
cnt_op = 0
for token in tokenList:
if token not in "ABCDEFGHIJKLMNOPQRST" and token not in "0123456789" and token not in "+-*/" and token not in "()":
raise RunTimeError("Wrong input")
if token in "()":
count_pa += 1
if token in "+-*/"
count_op += 1
if length - cnt_pa != 2 * cnt_op + 1:
raise RunTimeError("Wrong input")
opStack = Stack()
postFixList = []
prec = {}
prec['('] = 1
prec[')'] = 1
prec['+'] = 2
prec['-'] = 2
prec['*'] = 3
prec['/'] = 3
for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRST" or token in "0123456789":
postFixList.append(token)
elif token == "(":
opStack.push(token)
elif token == ")":
topToken = opStack.pop()
if 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)
示例11: infixToPostfix
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [as 别名]
def infixToPostfix(infixepr):
"""Create an empty stack called opstack for keeping operators. Create an empty list for output.
Convert the input infix string to a list by using the string method split.
Scan the token list from left to right.
If the token is an operand, append it to the end of the output list.
If the token is a left parenthesis, push it on the opstack.
If the token is a right parenthesis, pop the opstack until the corresponding left parenthesis is removed. Append each operator to the end of the output list.
If the token is an operator, *, /, +, or -, push it on the opstack. However, first remove any operators already on the opstack that have higher or equal precedence and append them to the output list.
When the input expression has been completely processed, check the opstack. Any operators still on the stack can be removed and appended to the end of the output list.
"""
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixepr.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)
示例12: Stack
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [as 别名]
from pythonds.basic.stack import Stack
s = Stack()
str = 'my python Script'
for i in range(len(str)):
s.push(str[i])
mystr=''
while not s.isEmpty():
mystr += s.peek()
s.pop()
print mystr
示例13: Stack
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [as 别名]
'''
Created on Jul 11, 2015
@author: Krish
'''
from pythonds.basic.stack import Stack
s= Stack()
print(s.isEmpty())
s.push(4)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())
示例14: Stack
# 需要导入模块: from pythonds.basic.stack import Stack [as 别名]
# 或者: from pythonds.basic.stack.Stack import peek [as 别名]
from pythonds.basic.stack import Stack
s = Stack()
assert(s.isEmpty())
s.push(4)
s.push('dog')
assert(s.peek() is 'dog')
s.push(True)
assert(s.size() is 3)
assert(s.isEmpty() is False)
s.push(8.4)
assert(s.pop() is 8.4)
assert(s.pop() is True)
assert(s.size() is 2)