本文整理汇总了Python中stack.Stack.isEmpty方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.isEmpty方法的具体用法?Python Stack.isEmpty怎么用?Python Stack.isEmpty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stack.Stack
的用法示例。
在下文中一共展示了Stack.isEmpty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: infix_to_postfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def infix_to_postfix(infixexpr):
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfix_list = []
token_list = infixexpr.split()
for token in token_list:
if token in "ABCDEFGIJKLMNOPQRSTUVWXYZ" or \
token in "0123456789":
postfix_list.append(token)
elif token == "(":
opStack.push(token)
elif token == ")":
topToken = opStack.pop()
while topToken != "(":
postfix_list.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.isEmpty()) and \
(prec[opStack.peek()] >= prec[token]):
postfix_list.append(opStack.pop())
opStack.push(token)
while not opStack.isEmpty():
postfix_list.append(opStack.pop())
return " ".join(postfix_list)
示例2: infixToPostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def infixToPostfix(infixexpr):
precedence = {}
precedence['*'] = 3
precedence['/'] = 3
precedence['+'] = 2
precedence['-'] = 2
precedence['('] = 1
opstack = Stack()
postfixLIst = [] # output list
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.isEmpty()) and (precedence[opstack.peek()] >= precedence[token]):
postfixLIst.append(opstack.pop())
opstack.push(token)
while not opstack.isEmpty():
postfixLIst.append(opstack.pop())
return " ".join(postfixLIst)
示例3: infixToPostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def infixToPostfix(infixexpr):
prec = {
'*': 3,
'/': 3,
'+': 2,
'-': 2,
'(': 1
}
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()
for token in tokenList:
if token in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or token in '0123456789':
# if token.isupper() or token.isdigit():
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.isEmpty()) and \
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)
while not opStack.isEmpty():
postfixList.append(opStack.pop())
return ' '.join(postfixList)
示例4: TestStack
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
class TestStack(unittest.TestCase):
def setUp(self):
self.st = Stack()
def tearDown(self):
self.st = None
def test_basic(self):
"""Basic test."""
self.assertTrue(self.st.isEmpty())
self.st.push(99)
self.assertFalse(self.st.isEmpty())
self.assertEqual(99, self.st.pop())
self.assertTrue(self.st.isEmpty())
def test_stackBehavior(self):
"""Ensure behaves like a stack."""
self.assertTrue(self.st.isEmpty())
self.st.push(99)
self.st.push(50)
self.st.push(25)
self.assertEqual(25, self.st.pop())
self.assertEqual(50, self.st.pop())
self.assertEqual(99, self.st.pop())
self.assertTrue(self.st.isEmpty())
示例5: parChecker
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def parChecker(symbolString):
s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol in "([{":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
top = s.pop()
if not matches(top,symbol):
balanced = False
index = index + 1
if balanced and s.isEmpty():
return True
else:
return False
示例6: test_s
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def test_s(self):
s = Stack()
self.assertTrue(s.isEmpty())
s.push(1)
self.assertFalse(s.isEmpty())
self.assertEqual(1, s.pop())
self.assertTrue(s.isEmpty())
示例7: parChecker
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def parChecker(filename):
liste = re.findall("<[/]*\w*>", oku(filename))
opens = ["<html>", "<body>", "<div>"]
closers = ["</html>", "</body>", "</div>"]
s = Stack()
balanced = True
index = 0
while index < len(liste) and balanced:
symbol = liste[index]
if symbol in opens:
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
top = s.peek()
if not matches(top, symbol, opens, closers):
balanced = False
else:
s.pop()
index = index + 1
if balanced and s.isEmpty():
return True
else:
print closers[opens.index(s.pop())], "eksik girdiniz"
return False
示例8: checker
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def checker(tags):
s = Stack()
balanced = True
i = 0
while i < len(tags) and balanced:
tag = tags[i]
if tag[0] != "/":
s.push(tag)
else:
if s.isEmpty():
balanced = False
else:
top = s.pop()
if not matches(top, tag):
balanced = False
i = i + 1
if balanced and s.isEmpty():
return True
else:
return False
示例9: is_brackets_blanced
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [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: checker
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def checker(tags):
s = Stack()
balanced = True
i = 0
while i < len(tags):
tag = tags[i]
if tag[0] != '/':
s.push(tag)
print "I: [%s] tagi acildi" % (tag)
else:
if s.isEmpty():
balanced = False
print "E: [%s] acilmamis" % (tag)
else:
top = s.peek()
if not matches(top, tag):
balanced = False
print "E: [%s] nin kapatmasi beklenirken [%s] geldi" % (top, tag)
else:
top = s.pop()
print "I: [%s] tagi kapandi" % (tag)
i = i + 1
if balanced and s.isEmpty():
print "HTML dengeli"
else:
if balanced:
while s.isEmpty():
print "[%s] kapatilmamis" % (s.pop())
print "HTML dengesiz"
示例11: balance_par_str_with_stack
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def balance_par_str_with_stack(str1):
s = Stack()
balanced = True
index = 0
while index < len(str1) and balanced:
symbol = str1[index]
if symbol == "(":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
s.pop()
index = index + 1
if balanced and s.isEmpty():
return True
else:
return False
开发者ID:harshilambagade,项目名称:Python-and-Algorithms-and-Data-Structures,代码行数:28,代码来源:banlance_parenthesis_str_stack.py
示例12: infixToPostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [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)
示例13: infixToPostFix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def infixToPostFix(expresssion):
prec = {}
prec["*"]=3
prec["/"]=3
prec["+"]=2
prec["-"]=2
prec["("]=1
opStack = Stack()
postfixList = []
tokenList = expresssion.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.isEmpty()) and (prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)
while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)
示例14: infixToPostfix
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def infixToPostfix(infixexpr):
prec = {} #poderia inicializar tudo aqui
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split() #poderia usar um list aqui, assim nao necessitaria dos espacos
for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789": #poderia estar tudo em uma so string, sem o or
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.isEmpty()) and \
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)
while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)
示例15: postfixEval
# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import isEmpty [as 别名]
def postfixEval(postfixExpr):
pe = postfixExpr.split()
s = Stack()
for p in pe:
if p in "0123456789":
s.push(p)
elif p in "+-*/":
if not s.isEmpty():
if not s.peek().isdigit():
return False
else:
return False
op2 = int(s.pop())
if not s.isEmpty():
if not s.peek().isdigit():
return False
else:
return False
op1 = int(s.pop())
s.push(str(doMath(p,op1,op2)))
print "unrecognized character"
return
if s.size()==1:
return s.peek().isdigit()
else:
return False