def get_rectangle(hist, sz):

    max_area = 0
    idx = 0

    st = Stack()

    while idx < sz:

        if st.top() is None or hist[idx] >= hist[st.top()]:
            st.push(idx)
            idx += 1
        else:
            top_ele = st.pop()
            current_area = hist[top_ele] * (idx if st.top() is None else
                                            (idx - st.top() - 1))
            #print "current_area  %s %s"%(hist[top_ele], current_area)
            max_area = max(max_area, current_area)

    #print "max= %s"%(max_area)
    while st.top() is not None:

        top_ele = st.pop()
        current_area = hist[top_ele] * (idx if st.top() is None else
                                        (idx - st.top() - 1))
        max_area = max(max_area, current_area)

    print "Max rectangele = %s" % (max_area)
Beispiel #2
0
 def postorder_travel2(node):
     """后序遍历,非递归法"""
     stack = Stack()
     while node is not None or not stack.is_empty():
         while node is not None:  # 下行循环,直到栈顶元素为空
             stack.push(node)
             node = node.left if node.left is not None else node.right
         node = stack.pop()
         print(node, end=" ")
         if not stack.is_empty() and stack.top(
         ).left == node:  # 栈非空且当前节点是栈顶的左子节点
             node = stack.top().right
         else:  # 没有右子树或右子树遍历完毕,强迫退栈
             node = None
def decode_str(str):

    l = str.__len__()

    int_stack = Stack()
    str_stack = Stack()

    for i in range(l):

        if str[i] >= '0' and str[i] <= '9':
            num = 0
            while (str[i] >= '0' and str[i] <= '9'):
                num = num * 10 + (ord(str[i]) - ord('0'))
                i += 1

            int_stack.push(num)

        elif str[i] == '(':  #push into char stack and the int multiplier

            if str[i - 1] >= '0' and str[i - 1] <= '9':
                str_stack.push('(')
            else:
                int_Stack.push(1)
                str_stack.push(')')

        elif str[i] == ')':  #process

            multiplier = int_stack.top()
            int_stack.pop()
            new_str = ''

            while (len(str_stack) != 0 and str_stack.top() != '('):
                new_str = str_stack.top() + new_str
                str_stack.pop()

            str_stack.pop()  #popping the (

            replicated_str = ''
            for j in range(multiplier):
                replicated_str += new_str

            for j in range(len(replicated_str)):
                str_stack.push(replicated_str[j])

        else:  #normal alphabets, just push them
            str_stack.push(str[i])

    print(str_stack)
Beispiel #4
0
def use_stack():

    st = Stack()

    for _ in range(10):
        st.push(random.randint(0, 50))

    st.get_stack()

    st.pop()

    print st.top()

    st.push(67)

    st.get_stack()
    if splitLines[num][0] in stackManipulationArray:
        try:
            stackManipulationModule(splitLines[num][0], splitLines[num][1])
        except:
            stackManipulationModule(splitLines[num][0], "")

    ####CONTROL FLOW####

    elif splitLines[num][0] == "label":
        pass

    elif splitLines[num][0] == "goto":
        num = labelIndex[splitLines[num][1]]

    elif splitLines[num][0] == "gofalse":
        if S.top() == 0:
            num = labelIndex[splitLines[num][1]]

    elif splitLines[num][0] == "gotrue":
        if S.top() != 0:
            num = labelIndex[splitLines[num][1]]

    elif splitLines[num][0] == "halt":
        break

    ####ARITHMETIC OPERATORS####

    elif splitLines[num][0] in arithArray:
        arithModule(splitLines[num][0])

    ####LOGICAL OPERATORS####