本文整理汇总了Python中stack.Stack.add方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.add方法的具体用法?Python Stack.add怎么用?Python Stack.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stack.Stack
的用法示例。
在下文中一共展示了Stack.add方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: StackAllTestCase
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import add [as 别名]
class StackAllTestCase(unittest.TestCase):
"""Comprehensive tests of (non-empty) Stack."""
def setUp(self):
"""Set up an empty stack."""
self.stack = Stack()
def tearDown(self):
"""Clean up."""
self.stack = None
def testAll(self):
"""Test adding and removeping multiple elements."""
for item in range(20):
self.stack.add(item)
assert not self.stack.is_empty(), \
'is_empty() returned True on a non-empty Stack!'
expect = 19
while not self.stack.is_empty():
assert self.stack.remove() == expect, \
('Something wrong on top of the Stack! Expected ' +
str(expect) + '.')
expect -= 1
示例2: SingletonTestCase
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import add [as 别名]
class SingletonTestCase(unittest.TestCase):
'''check whether adding a single item makes it appear in the top'''
def setUp(self):
self.s = Stack()
self.s.add('a')
def tearDown(self):
self.s = None
def testIsEmpty(self):
self.assertFalse(self.s.is_empty(), 'is_empty returned true on non-empty stack')
def testRemove(self):
top = self.s.remove()
self.assertEqual(top, 'a', 'The item at the top should have been "a" but was ' +
top + '.')
self.assertTrue(self.s.is_empty, ' stack with one element not empty after remove()')
示例3: TypicalTestCase
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import add [as 别名]
class TypicalTestCase(unittest.TestCase):
def setUp(self):
self.s = Stack()
def tearDown(self):
self.s = None
def testAll(self):
for item in range(20):
self.s.add(item)
self.assertFalse(self.s.is_empty(), 'stack should not be empty after adding item ' + str(item))
item = 19
while not self.s.is_empty():
top = self.s.remove()
self.assertEqual(top, item, 'wrong item at the top of the stack. Found' + str(top) + ' but expecting ' + str(item))
item -=1
示例4: StackEmptyTestCase
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import add [as 别名]
class StackEmptyTestCase(unittest.TestCase):
"""Test behaviour of an empty Stack."""
def setUp(self):
"""Set up an empty stack."""
self.stack = Stack()
def tearDown(self):
"""Clean up."""
self.stack = None
def testIsEmpty(self):
"""Test is_empty() on empty Stack."""
# it's hard to avoid \ continuation here.
assert self.stack.is_empty(), \
'is_empty returned False on an empty Stack!'
def testadd(self):
"""Test add to empty Stack."""
self.stack.add("foo")
assert self.stack.remove() == "foo", \
'Wrong item on top of the Stack! Expected "foo" here.'
示例5: isinstance
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import add [as 别名]
9
7
5
3
1
"""
for i in l:
s.add(i)
while not s.is_empty():
el = s.remove()
if isinstance(el, list):
for j in el:
s.add(j)
else:
print(el)
if __name__ == '__main__':
import doctest
doctest.testmod()
s = Stack()
imp = input('type a string: ')
while not imp == 'end':
s.add(imp)
inp = input('type a string: ')
while not s.is_empty():
print(s.remove())
list_stack([1, [3, [5, 7], 9], 11], Stack())
示例6: StackEmptyTestCaseByProf
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import add [as 别名]
class StackEmptyTestCaseByProf(unittest.TestCase):
"""Test behaviour of an empty Stack."""
def setUp(self):
"""Set up an empty stack."""
self.s1 = Stack()
self.s2 = Stack()
def tearDown(self):
"""Clean up."""
self.s1 = None
self.s2 = None
def test_IsEmpty(self):
"""Test is_empty() on empty Stack."""
# it's hard to avoid \ continuation here.
self.assertTrue(self.s1.is_empty())
def test_add(self):
"""Test add to empty Stack."""
self.s1.add("foo")
self.assertTrue(self.s1.remove() == "foo")
def test_equality(self):
"""test if two non-empty stack are equal"""
self.s1.add("foo")
self.s1.add("jijiji")
self.s2.add("foo")
self.s2.add("jijiji")
self.assertTrue(self.s1 == self.s2)
def test_not_equality(self):
"""test if two non-empty stack are equal"""
self.s1.add("foo")
self.s1.add("Joo")
self.s2.add("Joo")
self.s2.add("foo")
self.assertFalse(self.s1 == self.s2)