当前位置: 首页>>代码示例>>Python>>正文


Python Stack.isEmpty方法代码示例

本文整理汇总了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)
开发者ID:Munnu,项目名称:interactivepython,代码行数:33,代码来源:infixtopostfix.py

示例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)
开发者ID:dangBinh,项目名称:playground,代码行数:32,代码来源:infixToPostfix.py

示例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)
开发者ID:diccooo,项目名称:py_algo,代码行数:34,代码来源:infix_postfix.py

示例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())
开发者ID:abhishekgahlot,项目名称:python-data-structures,代码行数:27,代码来源:testStack.py

示例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
开发者ID:decoderc,项目名称:Python-DataStructures,代码行数:27,代码来源:parchecker.py

示例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())
开发者ID:khardi,项目名称:problem_solving_using_python,代码行数:9,代码来源:to_string_with_stack.py

示例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
开发者ID:gdemir,项目名称:pro-lang,代码行数:32,代码来源:html-check.py

示例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
开发者ID:semihdizdar,项目名称:ceng203vy,代码行数:27,代码来源:htmlChecker.py

示例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()
开发者ID:AlertBear,项目名称:Dstruter,代码行数:33,代码来源:example.py

示例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"
开发者ID:19ceng,项目名称:ceng203vy,代码行数:36,代码来源:htmlCheckerv2.py

示例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)
开发者ID:prijindal,项目名称:Interactivepython-Solutions,代码行数:31,代码来源:infixToPostfix.py

示例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)
开发者ID:rocco337,项目名称:PythonExercises,代码行数:32,代码来源:expressions.py

示例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)
开发者ID:andersonresende,项目名称:problem_solving,代码行数:32,代码来源:infix.py

示例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
开发者ID:kickbean,项目名称:LeetCode,代码行数:28,代码来源:stack_postfixEvaluation.py


注:本文中的stack.Stack.isEmpty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。