本文整理汇总了Python中stack.Stack类的典型用法代码示例。如果您正苦于以下问题:Python Stack类的具体用法?Python Stack怎么用?Python Stack使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Stack类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
my_stack = Stack()
for _ in range(10):
my_stack.push(randint(0, 9))
sort_stack(my_stack)
for _ in range(10):
print my_stack.pop()
示例2: converter
def converter(exp, fix="post"):
"""takes simple infix expression and converts to prefix or postfix"""
tokens = list(exp)
open_paren, close_paren = "(", ")"
if fix not in ["pre", "post"]:
raise ValueError("Must specify conversion as either 'pre' or 'post'")
if fix == "pre":
tokens.reverse()
open_paren, close_paren = ")", "("
s = Stack()
result = ''
for t in tokens:
if t == open_paren:
pass
elif t in ["*", "+", "-", "/"]:
s.push(t)
elif t == close_paren and not s.is_empty():
result += s.pop()
else:
result += t
if fix == "pre":
return result[::-1]
return result
示例3: __init__
def __init__(self):
'''
Initialize the queue with two stacks -- an incoming
and outgoing stack.
'''
self.incoming_stack = Stack()
self.outgoing_stack = Stack()
示例4: heightStack
def heightStack(height, nameStr):
h = Stack(nameStr)
while height != 0:
h.push(height)
height -= 1
return h
示例5: solution
def solution(self):
stck = Stack()
node = self.goal
while not node is None:
stck.push(node.board)
node = node.prev
return stck
示例6: test_push_pop
def test_push_pop(input_list):
from stack import Stack
new_stack = Stack()
for item in input_list:
new_stack.push(item)
for item in reversed(input_list):
assert new_stack.pop() == item
示例7: test_pop_2
def test_pop_2():
our_list = ["first", "second", "third", "fourth"]
our_stack = Stack(our_list)
assert our_stack.pop() == "fourth"
assert our_stack.pop() == "third"
assert our_stack.pop() == "second"
assert our_stack.pop() == "first"
示例8: __init__
def __init__(self):
'''
Initialize Queue
'''
self.stack1 = Stack()
self.stack2 = Stack()
示例9: test_count
def test_count(COUNT_LIST, count):
"""Test to see if stack count is correct."""
from stack import Stack
s = Stack()
for i in COUNT_LIST:
s.push(i)
assert s.size() == count
示例10: test_push
def test_push():
my_stack = Stack([1, 2, 3])
my_stack.push(4)
# assert my_stack.container.display() == (4, 3, 2, 1)
assert my_stack.pop() == 4
# check to make sure new stack wasn't initialized
assert my_stack.pop() == 3
示例11: test_empty
def test_empty():
"""Test case where an empty list is pushed to stack."""
from stack import Stack
stacky = Stack()
stacky.push([])
with pytest.raises(IndexError):
stacky.pop().get_data()
示例12: stack_sort
def stack_sort(s):
def find_max(s):
t = Stack()
m = None
while s.size > 0:
v = s.pop()
t.push(v)
if not m or v > m:
m = v
while t.size > 0:
v = t.pop()
if v != m:
s.push(v)
return m
sort = Stack()
while s.size > 0:
m = find_max(s)
sort.push(m)
while sort.size > 0:
s.push(sort.pop())
return s
示例13: test_push
def test_push():
"""Test stack push method."""
from stack import Stack
from stack import Node
stack = Stack()
stack.push(data[0])
assert isinstance(stack.head, Node)
示例14: __init__
class VisitOrder:
stack = None
level = 0
node_list = []
def __init__( self ):
self.stack = Stack()
def make_stack( self ):
while self.stack.length() > 0:
print "self.stack.length() ==> " + str( self.stack.length() )
print "Pop the stack ---> " + str( self.stack.pop().value )
def get_node( self, node_id ):
for n in self.node_list:
#print 'get_node ' + str( n.id )
if n.id == node_id:
return n
return None
def build_tree( self, node ):
self.level = self.level + 1
print "in build_tree level : " + str( self.level )
while len( node.value ) > 0:
node.left = self.stack.pop()
build_tree( node.left )
while len( node.value ) > 0:
node.right = self.stack.pop()
build_tree( node.right )
示例15: __init__
class MyQueue:
def __init__(self):
self.stack_in = Stack()
self.stack_out = Stack()
def enqueue(self, item):
if not self.stack_in.is_empty():
self.empty_stack(self.stack_in, self.stack_out)
self.stack_in.push(item)
def dequeue(self):
if not self.stack_out.is_empty():
self.empty_stack(self.stack_out, self.stack_in)
return self.stack_in.pop()
def empty_stack(self, stack1, stack2):
'''
Empty stack1 into stack2
'''
while not stack1.is_empty():
stack2.push(stack1.pop())
def print_mq(self):
self.empty_stack(self.stack_in, self.stack_out)
print self.stack_out