本文整理汇总了Python中Stack.is_empty方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.is_empty方法的具体用法?Python Stack.is_empty怎么用?Python Stack.is_empty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.is_empty方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: infix_to_postfix
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import is_empty [as 别名]
def infix_to_postfix(infix_expr):
token_list = infix_expr.split()
op_stack = Stack()
postfix_list = []
prec = {"*":3, "/":3, "+":2, "-":2, "(":1}
for token in token_list:
if token in "0123456789" or token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
postfix_list.append(token)
elif token == "(":
op_stack.push(token)
elif token == ")":
top_token = op_stack.pop()
while top_token != "(":
postfix_list.append(top_token)
top_token = op_stack.pop()
else:
while (not op_stack.is_empty()) and prec[op_stack.peek()] >= prec[token]:
postfix_list.append(op_stack.pop())
op_stack.push(token)
while (not op_stack.is_empty()):
postfix_list.append(op_stack.pop())
return " ".join(postfix_list)
#print infix_to_postfix("A + B + C + D")
#print infix_to_postfix("( A + B ) * ( C + D ) ")
#print infix_to_postfix("A + B * C + D")
示例2: Queue_from_Stacks
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import is_empty [as 别名]
class Queue_from_Stacks(object):
def __init__(self):
self.in_stack = Stack()
self.out_stack = Stack()
self.length = 0
def is_empty(self):
return self.length <= 0
def enqueue(self, element):
self.in_stack.push(element)
self.length += 1
def dequeue(self):
if(self.out_stack.is_empty()):
while (not(self.in_stack.is_empty())):
self.out_stack.push(self.in_stack.pop())
if (not(self.out_stack.is_empty())):
self.length -= 1
return self.out_stack.pop()
def __repr__(self):
return "IN: " + str(self.in_stack) + "\nOUT: " + str(self.out_stack)
示例3: __fromDecimal
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import is_empty [as 别名]
def __fromDecimal(self, base):
number = str(self.getDeciValue())
negative = True if "-" in number < 0 else False
if negative:
number = number[1:]
remainders = Stack()
separated_num = self.__separateFloat(str(float(number)))
integer = separated_num[0]
fraction = separated_num[1]
number = int(integer)
while number > 0:
remainder = number % base
remainders.push(int(remainder))
number = number // base
integer = "" if not remainders.is_empty() else "0"
while not remainders.is_empty():
try:
integer += Number.__DIGITS[remainders.pop()]
except IndexError as BaseError:
raise Exception( "Base outside range of representable digits.") from BaseError
fraction = "0." + fraction
fraction = self.__convertDecimalFraction(fraction, base)
output = integer + fraction
if negative:
output = "-" + output
self.__setValue(output)
示例4: to_binary_num
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import is_empty [as 别名]
def to_binary_num(decimal):
s = Stack()
while decimal > 0:
rem = decimal % 2
s.push(rem)
decimal /= 2
ans_string = ""
while not s.is_empty():
ans_string += str(s.pop())
return ans_string
示例5: print
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import is_empty [as 别名]
#https://www.cs.auckland.ac.nz/courses/compsci105ssc/resources/ProblemSolvingwithAlgorithmsandDataStructures.pdf
print("------------Starting Stack Testing------------")
#Stack
from Stack import *
s = Stack()
#Operations #Stack Contents #Return Value
print(s.is_empty()) #[] True
s.push(4) #[4]
s.push('dog') #[4,'dog']
print(s.top()) #[4,'dog'] 'dog'
print(s.push(True)) #[4,'dog',True]
print(s.size()) #[4,'dog',True] 3
print(s.is_empty()) #[4,'dog',True] False
print(s.push(8.4)) #[4,'dog',True,8.4]
print(s.pop()) #[4,'dog',True] 8.4
print(s.pop()) #[4,'dog'] True
print(s.size()) #[4,'dog'] 2
print("------------Starting Queue Testing------------")
#Queue
from Queue import *
q = Queue()