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


Python Stack.empty方法代码示例

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


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

示例1: Queue

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
class Queue(object):
    def __init__(self):
        self.q1 = Stack()
        self.q2 = Stack()

    def enqueue(self, v):
        self.q1.push(v)

    def empty(self):
        return self.q1.empty() and self.q2.empty()

    def size(self):
        return len(self.q1) + len(self.q2)

    def dequeue(self):
        if self.q2.empty():
            if self.q1.empty():
                return None

            while not self.q1.empty():
                self.q2.push(self.q1.poptop())

        return self.q2.poptop()

    def to_array(self):
        ret = []

        for i in self.q1.data:
            ret.append(i)
        for j in self.q2.data:
            ret.append(j)

        return ret
开发者ID:kubov,项目名称:akens,代码行数:35,代码来源:queue.py

示例2: test_empty_size

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
    def test_empty_size( self ):
        stack = Stack()

        self.assertTrue( stack.empty() )
        self.assertEqual( stack.size(), 0 )

        stack.push( 11 )
        self.assertFalse( stack.empty() )
        self.assertEqual( stack.size(), 1 )
开发者ID:danoli3,项目名称:pchGenerator,代码行数:11,代码来源:test_stack.py

示例3: test_push

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
def test_push():
    stack = Stack()
    stack.push(15)
    stack.push(6)
    stack.push(2)
    stack.push(9)
    assert(stack.empty() == False)
    assert(stack.peek() == 9)
开发者ID:prathamtandon,项目名称:python-algorithms-and-data-structures,代码行数:10,代码来源:stack_test.py

示例4: Queue

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
class Queue(object):

    def __init__(self):
        self.stack1 = Stack()
        self.stack2 = Stack()

    def enqueue(self, element):

        while not self.stack1.empty():
            self.stack2.push(self.stack1.pop())

        self.stack1.push(element)

        while not self.stack2.empty():
            self.stack1.push(self.stack2.pop())

    def dequeue(self):
        return self.stack1.pop()
开发者ID:platzhirsch,项目名称:algorithms,代码行数:20,代码来源:queue.py

示例5: Queue

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
class Queue(object):
    def __init__(self):
        self.q1 = Stack()
        self.q2 = Stack()

    def enqueue(self, v):
        self.q1.push(v)

    def empty(self):
        return self.q1.empty() and self.q2.empty()

    def dequeue(self):
        if self.q2.empty():
            if self.q1.empty():
                return None

            while not self.q1.empty():
                self.q2.push(self.q1.poptop())

        return self.q2.poptop()
开发者ID:kubov,项目名称:mazeinator,代码行数:22,代码来源:queue.py

示例6: solve

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
def solve(heights):
    """heights is an iterable containing the heights
    of the buildings ordered from east to west."""
    s = Stack()
    for h in heights:
        while not s.empty() and h > s.top():
            s.pop() # Keep popping if newly encountered building is taller than those before it.

        s.push(h)  # Add in this new building

    return s
开发者ID:lightalchemist,项目名称:algorithms,代码行数:13,代码来源:sunset.py

示例7: RPNCalc

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
class RPNCalc(dict):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stack = Stack()

    def ops(self):
        return self.keys()

    def solve(self, input_stack):
        while input_stack.size() > 0:
            value = input_stack.pop()
            if value in self.ops():
                op = self[value].function
                args = self.stack.pop(self[value].arity)
                args = args if isinstance(args, list) else [args]
                self.stack.push(op(*args))
            else:
                self.stack.push(value)
        result = self.stack[::]
        self.stack.empty()
        return result
开发者ID:tonnydourado,项目名称:rpn-repl,代码行数:23,代码来源:rpncalc.py

示例8: __init__

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
class Queue:
	def __init__(self):
		self.inStack  = Stack()
		self.outStack = Stack()

	def offer(self, val):
		self.inStack.push(val)

	def poll(self):
		if self.outStack.empty() :
			while True:
				val = self.inStack.pop()
				if val == None:
					break
				self.outStack.push(val)
		return self.outStack.pop()
开发者ID:hideki,项目名称:hideki,代码行数:18,代码来源:queue_by_stack.py

示例9: Stack

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
		stack.push(holder);

if __name__ == "__main__":
	print "= test stack operations =";
	stack = Stack();
	stack.push(1);
	stack.push(2);
	stack.push(3);
	stack.show();
	print "==============";
	print "pop", stack.pop();
	stack.show();
	print "==============";
	print "peek:", stack.peek();
	print "==============";
	while not stack.empty():
		print "pop", stack.pop();
	stack.show();
	print "===== done ======";

	print "====== start sorting ========";
	for i in range(1, 10):
		stack.push( randint(1,9) );
	stack.show();
	print "==============";

	# put sorted numbers into temp stack
	temp = Stack();
	while not stack.empty():
		holder = stack.pop();
		while (not temp.empty()) and (temp.peek() > holder):
开发者ID:killbug2004,项目名称:Snippets,代码行数:33,代码来源:sort.py

示例10: __init__

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
class Dag:
    def __init__( self, type = DagNode ):
        self._type = type
        self._nodes = {}
        self._stack = Stack()
        self._root = self._type( "root" )
        self._stack.push( self._root )

    def add( self, depth, object ):
        assert depth > 0, 'depth cant be less equal zero'

        if depth > self.__getDepth() + 1:
            raise Exception( "Wrong depth, stack: ", self.__getDepth(), ", depth: ", depth )

        depthDifference = self.__getDepth() - depth + 1

        for i in range( 0, depthDifference ):
            self._stack.pop()

        assert self._stack.empty() == False, 'stack cant be empty'

        header = self.__getOrCreate( object )

        if self.__areConnected( self._stack.top(), header ) == False:
            if self.__checkForCycle( self._stack.top(), header ) == False:
                self.__connect( self._stack.top(), header )

        self._stack.push( header )
        return header

    def get( self, object ):
        if object not in self._nodes:
            raise Exception( "object does not exist" )

        return self._nodes[ object ]

    def getNodes( self ):
        return self._nodes.values()

    def getRoot( self ):
        return self._root

    def deepPrint( self ):
        self._root.deepPrint()

    def __areConnected( self, node1, node2 ):
        return node2 in node1.getChildren()

    def __connect( self, node1, node2 ):
        node1.addChild( node2 )
        node2.addParent( node1 )

    def __getDepth( self ):
        return self._stack.size() - 1

    def __getOrCreate( self, object ):
        if object not in self._nodes:
            self._nodes[ object ] = self._type( object )

        return self._nodes[ object ]

    def __checkForCycle( self, parent, node ):
        result = self.__checkForCycleImpl( parent, node )

        node.setColorRecursively( DfsNode.White )

        return result

    def __checkForCycleImpl( self, parent, node ):
        if node.getColor() == DfsNode.Black:
            return False

        node.setColor( DfsNode.Black )

        if parent == node:
            return True

        for child in node.getChildren():
            if self.__checkForCycleImpl( parent, child ) == True:
                return True

        return False
开发者ID:danoli3,项目名称:pchGenerator,代码行数:84,代码来源:dag.py

示例11: parse_file

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import empty [as 别名]
	def parse_file(self, filename):
		scanner = Scanner_Class(filename)
		self.source_text = scanner.source()
		self.clear_messages()
		ret = 1
			
		stack = Stack()
		stack.push(1)
		current_token = scanner.next_token()
		
		while not stack.empty():
			# Skip comments
			if current_token.type == 8:
				current_token = scanner.next_token()
				continue		
			
			# Map all terminals to an integer
			token_number = self.translate(current_token)
			
			# Get head of the stack
			stacktop = stack.head.value;
			
			# Stats
			self.log("current_token: "+str(current_token.toString()), self.L_DEBUG)
			self.log("token_number: "+str(token_number), self.L_DEBUG)
			#self.log("stacktop: "+str(stacktop), self.L_DEBUG)
			#self.log("stack count: "+str(stack.count()), self.L_DEBUG)
			self.log("current stack: "+str(stack.toString()), self.L_DEBUG)

			# Non-terminal symbols
			if stacktop > 0:
				table_entry = self.next_table_entry(stacktop, abs(token_number))
				
				if table_entry == 98:
					self.log("Scan error: dropping "+current_token.value, self.L_ERROR)
					current_token = scanner.next_token()

				elif table_entry == 99:
					self.log("Pop Error: popping "+str(stack.head.value), self.L_ERROR)
					stack.pop()
				
				elif table_entry <= len(self.stack_pushes):
					self.log("Fire "+str(table_entry), 
						self.L_MATCH)
					stack.pop()
					
					for i in self.pushes(table_entry-1):
						if i != 0:
							stack.push(i)
				else:
					self.log("Error!", self.L_ERROR)
			
			# Terminals - Match and pop
			elif stacktop == token_number:
				self.log("Match and pop "+
					str(current_token.value), self.L_MATCH)
				stack.pop()
				current_token = scanner.next_token()
			
			# Terminals - No Match :(
			else:
				self.log("Error -- Not Accepted", self.L_MATCH)
				break;

			# Success message
			if stack.empty():
				self.log("Accept", self.L_MATCH)
				ret = 0
		
		# End While

		return ret
开发者ID:btelle,项目名称:cecs-444,代码行数:74,代码来源:parser_class.py


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