本文整理汇总了Python中stack.Stack.pop方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.pop方法的具体用法?Python Stack.pop怎么用?Python Stack.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stack.Stack
的用法示例。
在下文中一共展示了Stack.pop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: StackTest
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [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()
示例2: test_stack_count
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
def test_stack_count():
s = Stack()
assert_equals(s.count(), 0)
s.push(1)
assert_equals(s.count(), 1)
s.pop()
assert_equals(s.count(), 0)
示例3: infixToPostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [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)
示例4: AnimalStack
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
class AnimalStack():
def __init__(self):
self.catstack = Stack()
self.dogstack = Stack()
self.id = 0 # id used as timestamp
def push(self, data):
self.id += 1
if data is cat:
self.catstack.push(Animal(data, self.id))
else:
self.dogstack.push(Animal(data, self.id))
def pop(self):
cat = self.catstack.peek()
dog = self.catstack.peek()
if cat.id < dog.id:
return cat
else:
return dog
def popcat(self):
return self.catstack.pop()
def popdog(self):
return self.dogstack.pop()
示例5: main
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
def main():
groups = []
m = int(raw_input())
for i in range(m):
n = int(raw_input())
group = []
for j in range(n):
group.append(raw_input())
groups.append(group)
#print(groups)
for g in groups:
stack = Stack()
for item in g:
cmds = item.split()
if cmds[0] == 'push':
stack.push(int(cmds[1]))
elif cmds[0] == 'pop':
if stack.isEmpty():
print('error')
break
else:
stack.pop()
if not stack.isEmpty():
print(' '.join([str(i) for i in stack.items]))
示例6: test_stack
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
def test_stack(self):
stack = Stack()
self.assertEqual(stack.size, 0)
stack.push(4)
self.assertEqual(stack.peek(), 4)
stack.push(36)
self.assertEqual(stack.peek(), 36)
stack.push(3)
self.assertEqual(stack.peek(), 3)
stack.push(2)
self.assertEqual(stack.peek(), 2)
stack.push(1)
self.assertEqual(stack.peek(), 1)
self.assertEqual(stack.size, 5)
self.assertEqual(stack.pop(), 1)
self.assertEqual(stack.peek(), 2)
self.assertEqual(stack.pop(), 2)
self.assertEqual(stack.peek(), 3)
self.assertEqual(stack.pop(), 3)
self.assertEqual(stack.peek(), 36)
self.assertEqual(stack.pop(), 36)
self.assertEqual(stack.peek(), 4)
self.assertEqual(stack.pop(), 4)
self.assertEqual(stack.size, 0)
示例7: evaluate_postfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
def evaluate_postfix(expression, values):
# create a stack
stack = Stack()
# scan expression from left to right
# for each token
for token in expression:
# case 1: operator
# evalute top 2 things on stack and push result
if token in "+-*/":
operand1 = stack.pop()
operand2 = stack.pop()
if token == "+":
stack.push(operand1 + operand2)
elif token == "-":
stack.push(operand2 - operand1)
elif token == "*":
stack.push(operand1 * operand2)
elif token == "/":
stack.push(operand2 / operand1)
# case 2: operand
# add value of operand to the stack
else:
# look up the value of the token and put that on the stack
stack.push(values[token])
# return result (only thing left on the stack)
return stack.pop()
示例8: TestStack
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
class TestStack(unittest.TestCase):
def setUp(self):
self.s=Stack()
def test_afterCreationStackIsEmpty(self):
self.assertTrue(self.s.is_empty())
def test_afterOnePush_stackIsNotEmpty(self):
self.s.push(1)
self.assertFalse(self.s.is_empty())
def test_afterOnePushAndOnePop_stackIsEmpty(self):
self.s.push(1)
element = self.s.pop()
self.assertTrue(self.s.is_empty())
def test_pushedElementsArePoppedInReverseOrder(self):
self.s.push(1)
self.s.push(2)
self.s.push(3)
self.assertEquals(3, self.s.pop())
self.assertEquals(2, self.s.pop())
self.assertEquals(1, self.s.pop())
def test_popWhenEmptyShouldRaiseException(self):
self.assertRaises(StackEmptyError, self.s.pop)
def test_peekReturnsTopElementWithoutPopping(self):
self.s.push(0xAA)
self.assertEquals(0xAA, self.s.peek())
self.assertEquals(0xAA, self.s.peek())
def test_peekWhenEmptyShouldRaiseException(self):
self.assertRaises(StackEmptyError, self.s.peek)
示例9: is_brackets_blanced
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
def is_brackets_blanced(brackets):
"""
Whether the brackets are pairs
:param brackets: brackets string
:return: True or False
"""
blanced = True
index = 0
s = Stack()
b_left = ['(', '[', '{']
mapping_num_dict = {'(': 1,
')': 1,
'[': 2,
']': 2,
'{': 3,
'}': 3
}
while index < len(brackets) and blanced:
if brackets[index] in b_left:
s.push(brackets[index])
else:
if s.isEmpty():
blanced = False
else:
if mapping_num_dict[s.peek()] == mapping_num_dict[brackets[index]]:
s.pop()
else:
blanced = False
index += 1
return blanced and s.isEmpty()
示例10: Djikstra
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
class Djikstra(object):
def __init__(self):
self.values = Stack()
self.operators = Stack()
def parse(self, expression):
self.values.push(0)
for char in expression:
if char.isdigit():
self.values.push(float(char))
elif self.is_operator(char):
self.operators.push(char)
elif char == ')':
x = self.values.pop()
y = self.values.pop()
op = self.operators.pop()
result = self.apply_operator_to(op,x,y)
self.values.push(result)
return self.values.pop()
def is_operator(self, char):
return char in "+*-/"
def apply_operator_to(self,op,x,y):
if op in "/-":
x,y = y,x
return eval("{0}{1}{2}".format(x,op,y))
示例11: infixToPostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
def infixToPostfix(text):
S = Stack()
answer = []
operands = "qwertyuiopasdfghjklzxcvbnm".upper()
operators = "/*+-"
precedence = {"+": 0, "-": 0, "*": 1, "/": 1}
for i in text:
print(i)
if i in operands:
answer.append(i)
elif i == "(":
S.push(i)
elif i == ")":
while S.peek() != "(":
answer.append(S.pop())
S.pop()
elif i in operators:
if not S.isEmpty() and S.peek() != "(":
if precedence[i] < precedence[S.peek()]:
answer.append(S.pop())
S.push(i)
print(S.list)
print(answer)
print("")
while not S.isEmpty():
answer.append(S.pop())
return "".join(answer)
示例12: Queueky
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
class Queueky():
def __init__(self):
self.__s = Stack()
def enqueue(self, elem):
self.__s.push(elem)
def dequeue(self):
temp = Stack()
cache = Stack()
while self.__s.count() > 1:
temp.push(self.__s.pop())
while temp.count() > 0:
cache.push(temp.pop())
result = self.__s.pop()
self.__s = cache
return result
def is_empty(self):
return self.__s.is_empty()
def top(self):
return self.__s.top()
def count(self):
return self.__s.count()
示例13: postOrderIter
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
def postOrderIter(root):
''' iterative solution
We use a prev variable to keep track of the previously-traversed node. Lets assume curr is the current
node thats on top of the stack. When prev is currs parent, we are traversing down the tree.
In this case, we try to traverse to currs left child if available (ie, push left child to the stack).
If it is not available, we look at currs right child. If both left and right child do not exist (ie, curr is a leaf node),
we print currs value and pop it off the stack.
If prev is curr left child, we are traversing up the tree from the left. We look at curr right child.
If it is available, then traverse down the right child (ie, push right child to the stack), otherwise print curr value and pop it off the stack.
If prev is curr right child, we are traversing up the tree from the right. In this case, we print curr value and pop it off the stack.
'''
if root is None: return
s = Stack()
s.push(root)
prev = None
while not s.isEmpty():
curr = s.peek()
# traverse down
if prev is None or prev.left == curr or prev.right == curr:
if curr.left: s.push(curr.left)
if curr.right: s.push(curr.right)
# traverse up from left
elif prev == curr.left:
if curr.right: s.push(curr.right)
# traverse up from right
else:
print curr.value
s.pop()
prev = curr
示例14: __init__
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
class QueueBaller:
def __init__(self):
self.reversed_stack = Stack()
self.ordered_stack = Stack()
def __move_from(self, from_stack, to_stack):
pop_item = from_stack.pop()
while (pop_item):
to_stack.push(pop_item)
if from_stack.is_empty():
pop_item = False
else:
pop_item = from_stack.pop()
def enqueue(self, item):
self.reversed_stack.push(item)
def dequeue(self):
if self.ordered_stack.is_empty():
self.__move_from(self.reversed_stack, self.ordered_stack)
return self.ordered_stack.pop()
else:
return self.ordered_stack.pop()
def peek(self):
# I suppose we could check ordered_stack, but what if it's empty? do we run the move operation just for peek?
print(self.reversed_stack.peek())
print(self.ordered_stack.peek())
#return self.stack_one.peek()
def is_empty(self):
return self.reversed_stack.is_empty() and self.ordered_stack.is_empty()
示例15: StackWithMin
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import pop [as 别名]
class StackWithMin(object):
def __init__(self, top = None):
self.top = top
#store the min values
self._min = Stack(top)
def min(self):
retval = self._min.peek()
if retval is None:
retval = sys.maxsize
return retval
def push(self,data):
new_node = Node(data, self.top)
self.top = new_node
if data <= self.min():
self._min.push(data)
def pop(self):
if self.top:
retval = Node(self.top.data)
self.top = self.top.next
if retval.data == self.min():
self._min.pop()
return retval
def peek(self):
if self.top:
return self.top.data