本文整理汇总了Python中Stack.__len__方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.__len__方法的具体用法?Python Stack.__len__怎么用?Python Stack.__len__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.__len__方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: postEval
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import __len__ [as 别名]
def postEval(symbolString):
"""Given a postfix expression, evaluate it"""
if symbolString == None:
return None
tokens = symbolString.split()
stack = Stack()
for token in tokens:
# Is the token an integer?
if token.isdigit():
stack.push(int(token))
# otherwise, it must be an operator
elif stack.__len__() <= 1:
print("Ill-formed expression")
return None
else:
arg1 = stack.pop()
arg2 = stack.pop()
# Evaluate operator expressions (handles exceptions)
val = applyOp(token, arg1, arg2)
stack.push(val)
if not stack.__len__() == 1:
print("Ill-formed expression")
return None
return stack.pop()
示例2: __init__
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import __len__ [as 别名]
class Regex:
def __init__(self):
self.stack = Stack()
self.nfa_final = Node()
self.q_node = 0
self.array = []
def regex(self, string): # converts a regular expression to an nfa
for char in string: # traverses each character in the reg exp
if char == '&': # if the concatenate operation is used
assert self.stack.__len__() >= 2
nfa2 = self.stack.pop()
nfa1 = self.stack.pop()
self.stack.push(self.concatenate(nfa1, nfa2))
elif char == '+': # if the union operation is used
assert self.stack.__len__() >= 2
nfa1 = self.stack.pop()
nfa2 = self.stack.pop()
self.stack.push(self.union(nfa1, nfa2))
elif char == '*': # if kleene star is used
assert self.stack.__len__() >= 1
nfa = self.stack.pop()
self.stack.push(self.star(nfa))
else: # there is no operation, so push the newest character as an nfa
final = Node()
temp = Node()
temp.q = self.q_node
self.q_node += 1
final.q = self.q_node
self.q_node += 1
temp.create(final, char)
self.stack.push(temp)
self.nfa_final = self.stack.pop()
def concatenate(self, nfa1, nfa2): # concatenate function epsilon jumps from nfa1 to nfa2
temp1 = self.traverse_final(nfa1) # temp1 is the old final state of nfa1
temp1.final = False
nfa2.start = False
temp1.E2 = nfa2
return nfa1
def union(self, nfa1, nfa2): # union function
start_node = Node() # new start state
start_node.q = self.q_node
self.q_node += 1
start_node.start = True
final_node = Node() # new final state
final_node.q = self.q_node
self.q_node += 1
final_node.final = True
temp1 = self.traverse_final(nfa1) # old final state of nfa1
temp2 = self.traverse_final(nfa2) # old final state of nfa2
temp1.final = False
temp1.E2 = final_node
temp2.final = False
temp2.E2 = final_node
start_node.E = nfa1
start_node.E2 = nfa2
nfa1.start = False
nfa2.start = False
return start_node
def star(self, nfa): # kleene star function
start_node = Node() # new start state
start_node.q = self.q_node
self.q_node += 1
start_node.start = True
final_node = Node() # new final state
final_node.q = self.q_node
self.q_node += 1
final_node.final = True
temp = self.traverse_final(nfa) # old final state of nfa
temp.final = False
temp.E_star = nfa # loop back to old start state
temp.E2 = final_node
start_node.E = nfa
start_node.E2 = final_node
nfa.start = False
return start_node
@staticmethod
def traverse_final(nfa): # traverses through an nfa to the final state
temp = nfa
while not temp.is_final(): # while nfa is not at the final state
if temp.a is not None:
temp = temp.a
elif temp.b is not None:
temp = temp.b
elif temp.c is not None:
#.........这里部分代码省略.........