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