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


Python stack.Stack类代码示例

本文整理汇总了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()
开发者ID:SageBerg,项目名称:CrackingTheCodingInterviewProblems,代码行数:7,代码来源:3.6.py

示例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
开发者ID:AzizHarrington,项目名称:algo,代码行数:28,代码来源:fix_conversion.py

示例3: __init__

 def __init__(self):
     ''' 
     Initialize the queue with two stacks -- an incoming
     and outgoing stack.
     '''
     self.incoming_stack = Stack()
     self.outgoing_stack = Stack()
开发者ID:jd3johns,项目名称:dsa,代码行数:7,代码来源:queue_of_stacks.py

示例4: heightStack

def heightStack(height, nameStr):
    h = Stack(nameStr)
    while height != 0:
        h.push(height)
        height -= 1

    return h
开发者ID:karimov,项目名称:py-solved-tasks,代码行数:7,代码来源:tower_of_hanoi.py

示例5: solution

	def solution(self):
		stck = Stack()
		node = self.goal
		while not node is None:
			stck.push(node.board)
			node = node.prev
		return stck
开发者ID:deadmau915,项目名称:Npuzzle,代码行数:7,代码来源:solver.py

示例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
开发者ID:paulsheridan,项目名称:data-structures,代码行数:7,代码来源:test_stack.py

示例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"
开发者ID:corinnelhh,项目名称:data-structures,代码行数:7,代码来源:test_stack.py

示例8: __init__

	def __init__(self):
		'''
		Initialize Queue

		'''
		self.stack1 = Stack()
		self.stack2 = Stack()
开发者ID:ueg1990,项目名称:aids,代码行数:7,代码来源:queue_two_stacks.py

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

示例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
开发者ID:wilson0xb4,项目名称:cf-data-structures,代码行数:7,代码来源:test_stack.py

示例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()
开发者ID:WillWeatherford,项目名称:data-structures-1,代码行数:7,代码来源:test_stack.py

示例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
开发者ID:malloc47,项目名称:snippets,代码行数:25,代码来源:cci.py

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

示例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 )
开发者ID:Ross-Marshall,项目名称:Algorithms-Robert-Sedgewick,代码行数:32,代码来源:visit_order.py

示例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
开发者ID:bluciam,项目名称:ruby_versus_python,代码行数:25,代码来源:3_5.py


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