本文整理汇总了Python中stack.Stack.remove方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.remove方法的具体用法?Python Stack.remove怎么用?Python Stack.remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stack.Stack
的用法示例。
在下文中一共展示了Stack.remove方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: StackAllTestCase
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import remove [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 remove [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 remove [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: StackEmptyTestCaseByProf
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import remove [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)
示例5: StackEmptyTestCase
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import remove [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.'
示例6: isinstance
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import remove [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())
示例7: test_Stack_remove
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import remove [as 别名]
def test_Stack_remove(self):
l1 = Stack()
l1.push(2)
with self.assertRaises(NotImplementedError):
l1.remove(None)
示例8: LEXdfs
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import remove [as 别名]
def LEXdfs(graph, start):
"""
Does DFS search of graph, beginning at start.
Implemented from Algorithm 1 in
"Finding compact communities in large graphs"
by Creusefond, Largillier and Peyronnet.
http://dx.doi.org/10.1145/2808797.2808868
"""
#
# Create and initialize VISITED and LEX for all nodes
#
attrs = { VISITED: {},
LEX: {}}
node = graph.BegNI()
while node < graph.EndNI():
attrs[VISITED][node.GetId()] = 0
attrs[LEX][node.GetId()] = "0"
node.Next()
# initialize DFS variables
stack = Stack()
stack.append( start.GetId() )
i = 1
# do the search
while len(stack) > 0:
# print "stack:"
# print node_list_to_str(graph, stack, attrs)
# print
# print
# process top node
# print
# stack.print_all()
# print
node_id = stack.pop()
node = graph.GetNI(node_id)
attrs[VISITED][node_id] = i
array = []
# find unvisited neighbors of node
for in_id in range(node.GetOutDeg()):
out_id = node.GetOutNId(in_id)
out_node = graph.GetNI(out_id)
if attrs[VISITED][out_id] == 0:
# will raise exception if out_node not there
try:
# print "Trying to remove", node_to_str(graph, out_id, attrs)
stack.remove(out_id)
# print "Removed", node_to_str(graph, out_id, attrs)
except ValueError as e:
# expected to occur
pass
attrs[LEX][out_id] = str(i) + attrs[LEX][out_id]
array.append(out_id)
# end of unvisited neighbor
# end of neighbors
# print "Not sure if this is correct. Needs to randomize order for ties"
# print "Before"
# print node_list_to_str(graph, array, attrs)
array.sort(key = lambda n_id: attrs[LEX][n_id])
randomize_equal_neighbors(graph, array, attrs)
# print "After"
# print node_list_to_str(graph, array, attrs)
# print
# print
stack.extend(array)
i = i + 1
# print "stack:"
# print node_list_to_str(graph, stack, attrs)
# print
# print
# end of stack processing
return attrs