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


Python Stack.size方法代码示例

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


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

示例1: parenthe_check

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
def parenthe_check(symbol_string):
    """
    验证输入的string中括号是否平衡,即'('和')'是否成对出现,借助stack来实现比较简单
    """
    if not isinstance(symbol_string,str):
        print 'Warning! param should be string'
    s=Stack()
    index=0
    length=len(symbol_string)
    while index < length:
        symbol=symbol_string[index]
        if symbol == '(':
            s.push(symbol)
        elif symbol == ')':
            if s.size() > 0:
                s.pop()
            else:
                print '")" can not before "(", not balanced'
                return False
        index += 1

    if s.size() == 0:
        return True
    else:
        return False
开发者ID:ganlanshu,项目名称:datastructure,代码行数:27,代码来源:paren_balance.py

示例2: StackTest

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
class StackTest(unittest.TestCase):
    def setUp(self):
        self.simple_stack = Stack()

    def test_stack_push(self):
        self.assertEqual(self.simple_stack.isEmpty(), True)
        self.simple_stack.push(20)
        self.assertEqual(self.simple_stack.isEmpty(), False)

    def test_stack_pop(self):
        self.simple_stack.push('item')
        stack_size = self.simple_stack.size()
        self.assertEqual(self.simple_stack.pop(), 'item')
        self.assertNotEqual(self.simple_stack.size(), stack_size)
        self.assertEqual(self.simple_stack.size(), stack_size - 1)

    def test_stack_peek_returns_last_item(self):
        self.simple_stack.push('last')
        self.assertEqual(self.simple_stack.peek(), 'last')

    def test_stack_is_not_empty_after_push(self):
        self.simple_stack.push(10)
        self.assertFalse(self.simple_stack.isEmpty())

    def test_popping_beyond_stack_size(self):
        self.simple_stack.push(20)
        self.simple_stack.pop()
        with self.assertRaises(IndexError):
            self.simple_stack.pop()
开发者ID:salman-bhai,项目名称:DA_A_DS,代码行数:31,代码来源:test_stack.py

示例3: TestStack

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
class TestStack(unittest.TestCase):

	def setUp(self):
		self.obj = Stack()


	def tearDown(self):
		self.obj = Stack()


	def test_empty_stack(self):
		self.assertTrue(self.obj.is_empty())


	def test_empty_stack_raises_empty_error(self):
		self.assertRaises(StackEmptyError, lambda: self.obj.pop())


	def test_non_empty_stack(self):
		self.obj.push(1)

		self.assertEqual(self.obj.size(), 1)
		self.assertFalse(self.obj.is_empty())
		self.assertEqual(self.obj.pop(), 1)


	def test_push_multiple_items_stack(self):
		nitems = 10
		for i in range(nitems):
			self.obj.push(i)

		self.assertEqual(self.obj.size(), nitems)
		self.assertEqual(self.obj.pop(), nitems-1)
开发者ID:hpatel86,项目名称:data_structures,代码行数:35,代码来源:test_stack.py

示例4: infixToPostfix

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
def infixToPostfix(infixExpress):
    opstack=Stack() # to store operators,lower operators will be poped at first
    output=[]
    pre={'(':1,'+':2,'-':2,'^':3,'*':3,'/':3} # use int to hold precedence of operators,( holds the lowest
    if not isinstance(infixExpress,str):
        print ('Warning! expression should be str')
    length=len(infixExpress)
    index=0
    while index < length:
        character = infixExpress[index]
        if character.isdigit():
            output.append(character)
        elif character == '(':
            opstack.push(character)
        elif character in '+-*/^':
            while opstack.size() > 0 and pre[opstack.peek()] >= pre[character]: 
                output.append(opstack.pop())
            opstack.push(character) 
        elif character == ')':
            while opstack.peek() != '(':
                output.append(opstack.pop())    
            opstack.pop()
        index += 1
    
    while opstack.size() > 0: #字符串遍历完毕,若opstack中还有operator,就放入output
        output.append(opstack.pop())     
    
    return ' '.join(output)
开发者ID:ganlanshu,项目名称:datastructure,代码行数:30,代码来源:infix2Postfix.py

示例5: test_empty_size

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [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

示例6: TwoStackQueue

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

    def __init__(self):
        self.enqueue_stack = Stack()
        self.dequeue_stack = Stack()

    def enqueue(self, val):
        if self.dequeue_stack.size() != 0:
            while self.dequeue_stack.size() != 0:
                self.enqueue_stack.push(self.dequeue_stack.pop())
        self.enqueue_stack.push(val)

    def dequeue(self):
        if self.dequeue_stack.size() == 0:
            if self.enqueue_stack.size() == 0:
                msg = 'Queue is empty'
                raise Warning(msg)
            else:
                while self.enqueue_stack.size() > 1:
                    self.dequeue_stack.push(self.enqueue_stack.pop())
                return self.enqueue_stack.pop()
        else:
            return self.dequeue_stack.pop()

    def size(self):
        if self.enqueue_stack.size() == 0:
            return self.dequeue_stack.size()
        else:
            return self.enqueue_stack.size()

    def is_empty(self):
        if self.size() == 0:
            return True
        else:
            return False
开发者ID:jwhite007,项目名称:DataStructures,代码行数:37,代码来源:queue.py

示例7: test_push

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

        stack.push( 11 )
        self.assertEqual( stack.top(), 11 )
        self.assertEqual( stack.size(), 1 )

        stack.push( 22 )
        self.assertEqual( stack.top(), 22 )
        self.assertEqual( stack.size(), 2 )

        stack.push( 33 )
        self.assertEqual( stack.top(), 33 )
        self.assertEqual( stack.size(), 3 )
开发者ID:danoli3,项目名称:pchGenerator,代码行数:16,代码来源:test_stack.py

示例8: checkpars

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
def checkpars(word):
	s = Stack()
	for letter in word:
		if letter == '(':
			s.push(1)
		elif letter == ')':
			if s.size() == 0:
				return False
			else:
				s.pop()
	if s.size() == 0:
		return True
	else:
		return False
开发者ID:gfyhui,项目名称:summer_algorithms_practice,代码行数:16,代码来源:parchecker.py

示例9: test_count

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
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,代码行数:9,代码来源:test_stack.py

示例10: postfixEval

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
def postfixEval(postfixExpr):
    pe = postfixExpr.split()
    s = Stack()
    for p in pe:
        if p in "0123456789":
            s.push(p)
        elif p in "+-*/":
            if not s.isEmpty():
                if not s.peek().isdigit():
                    return False
            else:
                return False
            op2 = int(s.pop())
            if not s.isEmpty():
                if not s.peek().isdigit():
                    return False
            else:
                return False
            op1 = int(s.pop())
            s.push(str(doMath(p,op1,op2)))
        print "unrecognized character"
        return 
    if s.size()==1:
        return s.peek().isdigit()
    else:
        return False
开发者ID:kickbean,项目名称:LeetCode,代码行数:28,代码来源:stack_postfixEvaluation.py

示例11: test_pop

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
def test_pop(COUNT_LIST, result):
    """Test to see if last value in stack is popped out."""
    from stack import Stack
    s = Stack()
    for i in COUNT_LIST:
        s.push(i)
    s.pop()
    assert s.size() == 4
开发者ID:tesmonrd,项目名称:data_structures,代码行数:10,代码来源:test_stack.py

示例12: StackTestCase

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
class StackTestCase(unittest.TestCase):

    def setUp(self):
        self.stack = Stack()

    def test_isEmpty(self):
        self.assertEqual(self.stack.isEmpty(),True)

    def test_pop(self):
        self.stack.push(1)
        self.stack.push('dog')
        self.assertEqual(self.stack.pop(),'dog')
        self.assertEqual(self.stack.size(),1)

    def test_peek(self):
        self.stack.push(1)
        self.stack.push('dog')
        self.assertEqual(self.stack.peek(),'dog')
        self.assertEqual(self.stack.size(),2)
开发者ID:neo1989,项目名称:interactive,代码行数:21,代码来源:test_stack.py

示例13: convert

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
def convert(num):
    # works only for positive numbers
    s = Stack()
    while num > 0:
        s.push(num % 2)
        num = num // 2
    binary = 0
    power = s.size() - 1
    while not s.isEmpty():
        binary += s.pop() * 10**power
        power -= 1
    return binary
开发者ID:bhsaurabh,项目名称:python-practice,代码行数:14,代码来源:decimal_to_binary.py

示例14: __init__

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
class MyQueue:
    def __init__(self):
        self.new_elements = Stack()
        self.old_elements = Stack()
    
    def enqueue(self, element):
        self.new_elements.push(element)
    
    def dequeue(self, element):
        if self.old_elements.is_empty():
            self.move_elements()
        
        return self.old_elements.pop()
    
    def move_elements(self):
        while not self.new_elements.is_empty():
            element = self.new_elements.pop()
            self.old_elements.push(element)
    
    def size(self):
        return self.new_elements.size() + self.old_elements.size()
开发者ID:aCrawfor,项目名称:diary_of_programming_puzzles,代码行数:23,代码来源:my_queue.py

示例15: build_tree

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import size [as 别名]
def build_tree(math_exp_string):
    """
    This method is used to build a tree from given input mathematical expression.
    Following consideration has been taken
    1. higher order operations are given with complete parenthesis ex. 1 - (2*3)
    2. add left and right parenthesis if not given ex. (1 - (2 * 3))
    3. print error message for any exception

    """
    if not validate_math_exp(math_exp_string):
        raise InvalidInput('Validation Error, one or more parenthesis are not closed properly')
    
    exp_list = filter_exp_list(math_exp_string)
    stack = Stack()
    current_node = Tree()

    for token in exp_list:

        if token == '(':
            current_node.add_child()
            stack.push(current_node)
            current_node = current_node.get_newborn_child()

        elif token == ')':
            if stack.size():
                current_node = stack.pop()

        elif token in operator_map.keys():
            if current_node.get_val():
                if current_node.get_val() == token:
                    current_node.add_child()
                    stack.push(current_node)
                    current_node = current_node.get_newborn_child()
                else:
                    parent = Tree(token)
                    parent.update_child(current_node)
                    parent.add_child()
                    stack.push(parent)
                    current_node = parent.get_newborn_child()
            else:
                current_node.set_val(token)
                current_node.add_child()
                stack.push(current_node)
                current_node = current_node.get_newborn_child()

        else:
            try:
                current_node.set_val(float(token))
            except ValueError, e:
                logging.info(e.message)
                current_node.set_val(token)
            current_node = stack.pop()
开发者ID:shaikhul,项目名称:eval_math_exp_using_tree,代码行数:54,代码来源:tree_manipulation.py


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