本文整理汇总了Python中Stack.push方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.push方法的具体用法?Python Stack.push怎么用?Python Stack.push使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.push方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parChecker
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def parChecker(symbolString):
s=Stack()
balanced = True # default
index =0
while index < len(symbolString) and balanced:
symbol=symbolString[index]
# if (,{,[ push onto stack
if symbol in "({[":
s.push(symbol)
else:
# if stack is empty, imbalance
if s.isEmpty():
balanced = False
else:
top=s.pop()
if not matches(top,symbol):
balanced = False
index = index+1
# balanced and no ( present
if balanced and s.isEmpty():
return True
# otherwise unbalanced ( present
else:
return False
示例2: dectobin
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def dectobin(n):
s=Stack()
while (n>0):
m=n%2
s.push(m)
n=int(n/2)
return s
示例3: __fromDecimal
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [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: buildParseTree
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def buildParseTree(exp_string):
exp_list = exp_string.split()
parent_stack = Stack()
root = BinaryTree(None)
current_node = root
for token in exp_list:
if token == "(":
current_node.insertLeft(None)
parent_stack.push(current_node)
current_node = current_node.getLeftChild()
elif token in "+-*/":
current_node.setRootVal(token)
parent_stack.push(current_node)
current_node.insertRight(None)
current_node = current_node.getRightChild()
elif token == ")":
if parent_stack.size() > 0:
parent_node = parent_stack.pop()
current_node = parent_node
else:
current_node.setRootVal(float(token))
if parent_stack.size() > 0:
parent_node = parent_stack.pop()
current_node = parent_node
return root
示例5: NewStack
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
class NewStack():
def __init__(self):
self.Stack = Stack()
self.minStack = Stack()
self.tempMin = None
def isEmpty(self):
return self.Stack.isEmpty()
def push(self, item):
if self.tempMin == None or item < self.tempMin:
self.tempMin = item
self.Stack.push(item)
self.minStack.push(self.tempMin)
def pop(self):
self.minStack.pop()
return self.Stack.pop()
def min(self):
return self.minStack.peek()
def peek(self):
return self.Stack.peek()
def size(self):
return self.Stack.size()
示例6: postEval
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [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()
示例7: Queue_from_Stacks
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [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)
示例8: infix_to_postfix
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [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")
示例9: convertStack
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def convertStack(self,stack,newBase,oldBase):
revStack = Stack()
while stack.length() != 0:
revStack.push(stack.pop())
while revStack.length() != 0:
stack.push(Operation.__convertFromDecimal(
Operation.__convertToDecimal(revStack.pop(),oldBase),newBase))
del revStack
示例10: testPop
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def testPop(self):
stack = Stack()
stack.push(0)
stack.push(5)
stack.push(2)
item = stack.pop()
self.assertEquals(item, 2)
self.assertEquals(stack.data, [0, 5])
示例11: revrse
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def revrse(expr):
revsd=[]
stck=Stack()
for each in expr:
stck.push(each)
while stck.isEmpty() is not True:
revsd.append(stck.pop())
print ''.join(revsd)
示例12: SortStack
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def SortStack(mystack):
HelpStack = Stack()
while not mystack.isEmpty():
MaxValue = mystack.pop()
while not HelpStack.isEmpty() and HelpStack.peek() > MaxValue:
mystack.push(HelpStack.pop())
HelpStack.push(MaxValue)
return HelpStack
示例13: to_binary_num
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [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
示例14: dec_to_base
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def dec_to_base(dec, base):
digits = "0123456789ABCDEF"
s = Stack()
while dec > 0:
s.push(dec % base)
dec = dec / base
yeni = ""
while not s.isEmpty():
yeni = yeni + digits[s.pop()]
return yeni
示例15: decTo
# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import push [as 别名]
def decTo(self):
digits = "0123456789ABCDEF"
remstack = Stack()
while self.val > 0:
rem = self.val % self.base
remstack.push(rem)
self.val = self.val / self.base
newOne = ""
while not remstack.isEmpty():
newOne = newOne + digits[remstack.pop()]
return newOne