本文整理汇总了Python中stack.Stack.is_empty方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.is_empty方法的具体用法?Python Stack.is_empty怎么用?Python Stack.is_empty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stack.Stack
的用法示例。
在下文中一共展示了Stack.is_empty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: infixToPostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
def infixToPostfix(infixexpr):
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()
for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
postfixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.is_empty()) and \
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)
while not opStack.is_empty():
postfixList.append(opStack.pop())
return " ".join(postfixList)
示例2: infixTopostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
def infixTopostfix(infixExpression):
reg = re.search(r'[[email protected]#$%^&\t\s\{\[\]\}]', infixExpression)
if reg:
raise Exception("uncorrect infix expression")
prec = {}
prec["^"] = 4
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opstack =Stack()
postfixList = []
tokenList = re.findall(r'[^[email protected]#$%^&\t\s\{\[\]\}]', infixExpression)
for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
postfixList.append(token)
elif token is "(":
opstack.push(token)
elif token is ")":
topToken = opstack.pop()
while topToken != "(":
postfixList.append(topToken)
topToken = opstack.pop()
else:
while (not opstack.is_empty()) and (prec[opstack.peek()] >= prec[token]):
postfixList.append(opstack.pop())
opstack.push(token)
while not opstack.is_empty():
postfixList.append(opstack.pop())
return " ".join(postfixList)
示例3: infix_to_postfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
def infix_to_postfix(self,s):
tmp=Stack()
for i in s:
if i.isdigit():
self.postfix.push(i)
elif i !=')':
if tmp.is_empty():
tmp.push(i)
else:
if self.pri[i]>self.pri[tmp.top()]:
tmp.push(i)
else:
while not tmp.is_empty():
t=tmp.top()
if t=='(':
break
self.postfix.push(tmp.pop())
tmp.push(i)
else:
t=tmp.pop()
while t!='(':
self.postfix.push(t)
t=tmp.pop()
while not tmp.is_empty():
self.postfix.push(tmp.pop())
示例4: test_is_empty
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
def test_is_empty(self):
s = Stack()
self.assertEquals(s.is_empty(), True)
s.push("a")
self.assertEquals(s.is_empty(), False)
s.pop()
self.assertEquals(s.is_empty(), True)
示例5: MyQueue
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
class MyQueue(object):
def __init__(self):
self.stack_one = Stack()
self.stack_two = Stack()
def enqueue(self, item):
if self.stack_one.is_empty():
self.stack_one.push(item)
return
while not self.stack_one.is_empty():
temp = self.stack_one.top()
self.stack_one.pop()
self.stack_two.push(temp)
self.stack_one.push(item)
while not self.stack_two.is_empty():
temp = self.stack_two.top()
self.stack_two.pop()
self.stack_one.push(temp)
def dequeue(self):
self.stack_one.pop()
示例6: toPostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
def toPostfix(exp): #Example Expression 'A + (B * C) + D', don't forget spaces !!!
op_prec={'*':3,'/':3,'+':2,'-':3,'(':1}
s=Stack()
tokens=exp.split()
op_list=[]
op_parn=['+','-','/','*','(',')']
for symbol in tokens:
if symbol not in op_parn:
op_list.append(symbol)
elif symbol == '(':
s.push(symbol)
elif symbol == ')':
top_token=s.pop()
while top_token != '(':
op_list.append(top_token)
top_token=s.pop()
else:
while((not s.is_empty()) and op_prec[symbol]<=op_prec[s.peek()]):
op_list.append(s.pop())
s.push(symbol)
while(not s.is_empty()):
op_list.append(s.pop())
return "".join(op_list)
示例7: TestStack
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [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)
示例8: inf_to_post
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
def inf_to_post(expr):
pre = { "/" : 3,
"*" : 3,
"+" : 2,
"-" : 2,
"(" : 1 }
result = []
s = Stack()
exp = expr.split()
for i in exp:
# print result,s
if i.isalpha() or i.isdigit() :
result.append(i)
elif i == '(' :
s.push(i)
elif i == ')':
top_token = s.pop()
while top_token != '(':
result.append(top_token)
top_token = s.pop()
else :
while (not s.is_empty()) and (pre[s.peek()] >= pre[i]) :
# print pre[s.peek()],pre[i]
result.append(s.pop())
s.push(i)
while not s.is_empty():
result.append(s.pop())
return "".join(result)
开发者ID:josejibin,项目名称:Problem_Solving_with_Algorithms_Data_Structures-Book,代码行数:32,代码来源:infix_to_postfix.py
示例9: __init__
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [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()
示例10: StackAllTestCase
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [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
示例11: Stream
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
class Stream(object):
def __init__(self, coreset_alg, leaf_size, coreset_size, k):
self.coreset_alg = coreset_alg
self.leaf_size = leaf_size
self.last_leaf = []
self.coreset_size = coreset_size
self.stack = Stack()
self.coreset_size = coreset_size
self.k = k
def _merge(self, pset1, pset2):
points = np.vstack([pset1.points, pset2.points])
weights = np.hstack([pset1.weights, pset2.weights])
cset = self.coreset_alg(points, self.k, weights)
coreset, weights = cset.compute(self.coreset_size)
return WeightedPointSet(coreset,weights)
def _add_leaf(self, points, weights):
if weights is None:
weights = np.ones((points.shape[0])).ravel()
self._insert_into_tree(WeightedPointSet(points, weights))
def _is_correct_level(self, level):
if self.stack.is_empty():
return True
elif self.stack.top().level > level:
return True
elif self.stack.top().level == level:
return False
else:
raise Exception("New level should be smaller")
def _insert_into_tree(self, coreset):
level = 1
while not self._is_correct_level(level):
last = self.stack.pop()
coreset = self._merge(last.coreset, coreset)
level += 1
self.stack.push(StackItem(coreset, level))
def add_points(self, points):
"""Add a set of points to the stream.
If the set is larger than leaf_size, it is split
into several sets and a coreset is constructed on each set.
"""
for split in np.array_split(points, self.leaf_size):
self._add_leaf(split,None)
def get_unified_coreset(self):
solution = None
while not self.stack.is_empty():
coreset = self.stack.pop().coreset
if solution is None:
solution = coreset
else:
solution = self._merge(solution, coreset)
return solution.points, solution.weights
示例12: __init__
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
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
示例13: __to_postfix_tokens
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
def __to_postfix_tokens(self):
postfix_expression = []
operators = Stack()
for token in self.tokens:
if token not in "+-*/()":
postfix_expression.append(token)
continue
if token == "(":
operators.push(token)
elif token == ")":
while not operators.is_empty() and operators.peek() != "(":
postfix_expression.append(operators.pop())
if not operators.is_empty():
operators.pop() # Pops the "("
elif token in "+-":
while not operators.is_empty() and operators.peek() in "+-*/":
postfix_expression.append(operators.pop())
operators.push(token)
elif token in "*/":
while not operators.is_empty() and operators.peek() in "*/":
postfix_expression.append(operators.pop())
operators.push(token)
while not operators.is_empty():
postfix_expression.append(operators.pop())
return postfix_expression
示例14: infixEval
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [as 别名]
def infixEval(infixExpression):
prec = {}
pec["^"] = 4
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec['-'] = 2
prec["("] = 1
opstack = Stack()
operandstack = Stack()
tokenList = infixExpression.split()
for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
operandstack.push(token)
elif token is "(":
opstack.push(token)
elif token is ")":
topToken = opstack.pop()
while topToken != "(":
operand2 = operandstack.pop()
operand1 = operandstack.pop()
result = doMath(topToken, operand1, operand2)
operandstack.push(result)
topToken = opstack.pop()
elif opstack.is_empty():
opstack.push(token)
示例15: TestStack
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import is_empty [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)