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


Python Stack.peek方法代码示例

本文整理汇总了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
开发者ID:hahayonghuming,项目名称:python,代码行数:32,代码来源:parsetree.py

示例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)
开发者ID:teddymcw,项目名称:pythonds_algos,代码行数:32,代码来源:stacks.py

示例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)
开发者ID:hvu53,项目名称:algorithms1,代码行数:33,代码来源:2_stack_infix.py

示例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
开发者ID:eladnoor,项目名称:small-molecule-regulation,代码行数:31,代码来源:bool_parser.py

示例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)
开发者ID:gr8h,项目名称:competitive_programming_py,代码行数:27,代码来源:revstring.py

示例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)
开发者ID:superdue,项目名称:code,代码行数:32,代码来源:infix_prefix_and_postfix_expressions.py

示例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)
开发者ID:yuelingjiang,项目名称:workspace,代码行数:27,代码来源:algorithm2.py

示例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
开发者ID:ssubramanian90,项目名称:Cracking-the-tech-interview,代码行数:10,代码来源:3-6.py

示例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
开发者ID:samjia533332,项目名称:Python_learning,代码行数:58,代码来源:infix_to_post.py

示例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)
开发者ID:hahayonghuming,项目名称:python,代码行数:50,代码来源:infix_to_post.py

示例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)
开发者ID:andersy005,项目名称:Python-Quest,代码行数:46,代码来源:infixToPostfiix.py

示例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
开发者ID:andersy005,项目名称:Python-Quest,代码行数:20,代码来源:string_reverse.py

示例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())
开发者ID:Krish-Mahajan,项目名称:Python-Codes,代码行数:22,代码来源:stackTest.py

示例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)
开发者ID:superdue,项目名称:code,代码行数:17,代码来源:stacktest.py


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