本文整理匯總了Python中stack.Stack方法的典型用法代碼示例。如果您正苦於以下問題:Python stack.Stack方法的具體用法?Python stack.Stack怎麽用?Python stack.Stack使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類stack
的用法示例。
在下文中一共展示了stack.Stack方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: is_paren_balanced
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def is_paren_balanced(paren_string):
s = Stack()
is_balanced = True
index = 0
while index < len(paren_string) and is_balanced:
paren = paren_string[index]
if paren in "([{":
s.push(paren)
else:
if s.is_empty():
is_balanced = False
else:
top = s.pop()
if not is_match(top, paren):
is_balanced = False
index += 1
if s.is_empty() and is_balanced:
return True
else:
return False
示例2: __init__
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def __init__(self, embs, relations, tokens, dependencies, alignments, oracle, hooks, variables, stage, rules):
self.semicol_gen_and = False
self.hooks = hooks
self.variables = variables
self.buffer = Buffer(embs, tokens, alignments)
self.embs = embs
self.stage = stage
self.dependencies = Dependencies([(self.buffer.tokens[i1],label,self.buffer.tokens[i2]) for (i1,label,i2) in dependencies])
self.stack = Stack(embs)
self.oracle = oracle
self.rules = rules
if relations is not None:
self.gold = Relations(copy.deepcopy(relations))
else:
self.gold = None
self.sentence = " ".join([t.word for t in tokens])
self.counter = 0
示例3: treeParser
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def treeParser(string: str)->TreeNode:
string = string.split()
tree = TreeNode()
stack: Stack = Stack()
currentTree: TreeNode = tree
stack.push(tree)
for item in string:
if item == "(":
currentTree.insertLeft()
stack.push(currentTree)
currentTree = currentTree.getLeftChild()
elif item == ")":
currentTree = stack.pop()
elif item in "+-*/":
currentTree.setRootValue(item)
currentTree.insertRight()
stack.push(currentTree)
currentTree = currentTree.getRightChild()
elif pattern.match(item):
currentTree.setRootValue(int(item))
currentTree = stack.pop()
else:
raise ValueError()
return tree
開發者ID:ivanmmarkovic,項目名稱:Problem-Solving-with-Algorithms-and-Data-Structures-using-Python,代碼行數:27,代碼來源:exercise04-inorder.py
示例4: div_by_2
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def div_by_2(dec_num):
if dec_num == 0:
return 0
s = Stack()
while dec_num > 0:
remainder = dec_num % 2
s.push(remainder)
dec_num = dec_num // 2
bin_num = ""
while not s.is_empty():
bin_num += str(s.pop())
return bin_num
示例5: test_push
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_push(self):
test_stack = stack.Stack()
with self.assertRaises(ValueError):
test_stack.peek()
test_value = 22
test_stack.push(test_value)
self.assertEquals(test_value, test_stack.peek())
示例6: test_pop
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_pop(self):
test_stack = stack.Stack()
test_value = 42
test_stack.push(test_value)
self.assertEquals(test_value, test_stack.pop())
示例7: test_peek
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_peek(self):
test_stack = stack.Stack()
test_value = 22
test_stack.push(test_value)
self.assertEquals(test_value, test_stack.peek())
示例8: test_size
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_size(self):
test_stack = stack.Stack()
test_stack.push('Woo!')
test_stack.push('This is fun!')
test_stack.push('Wow! I love reading UnitTests!')
self.assertEquals(3, test_stack.size)
示例9: __init__
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def __init__(self, **kwargs):
self.name = ''
self.tx_fields = []
self.tx_serializer = transaction.TransactionSerializer
self.block_header_fields = list(_bitcoin_header_fields)
self.block_fields = list(_bitcoin_block_fields)
self.opcode_overrides = list(_bitcoin_opcode_overrides)
self.script_engine_cls = stack.Stack
self.opcode_names = dict(OPCODE_NAMES)
self.opcodes_by_name = dict(OPCODES_BY_NAME)
self.disabled_opcodes = list(DISABLED_OPCODES)
for k, v in kwargs.items():
setattr(self, k, v)
示例10: set_opcode_overrides
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def set_opcode_overrides(ops):
"""Set the overridden behavior of specified opcodes.
This affects all Stack steps that run afterward.
"""
opcodes.set_overridden_opcodes(ops)
示例11: test_init
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_init(self):
s = Stack()
assert s.peek() is None
assert s.length() == 0
assert s.is_empty() is True
示例12: test_init_with_list
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_init_with_list(self):
s = Stack(['A', 'B', 'C'])
assert s.peek() == 'C'
assert s.length() == 3
assert s.is_empty() is False
示例13: test_length
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_length(self):
s = Stack()
assert s.length() == 0
s.push('A')
assert s.length() == 1
s.push('B')
assert s.length() == 2
s.pop()
assert s.length() == 1
s.pop()
assert s.length() == 0
示例14: test_push
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_push(self):
s = Stack()
s.push('A')
assert s.peek() == 'A'
assert s.length() == 1
s.push('B')
assert s.peek() == 'B'
assert s.length() == 2
s.push('C')
assert s.peek() == 'C'
assert s.length() == 3
assert s.is_empty() is False
示例15: test_peek
# 需要導入模塊: import stack [as 別名]
# 或者: from stack import Stack [as 別名]
def test_peek(self):
s = Stack()
assert s.peek() is None
s.push('A')
assert s.peek() == 'A'
s.push('B')
assert s.peek() == 'B'
s.pop()
assert s.peek() == 'A'
s.pop()
assert s.peek() is None