本文整理汇总了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