コード例 #1
0
def LargestRectangleInHistogram(hist):
    s = Stack()
    i = 0
    max_area = 0

    # Scan through Histogram
    while i < len(hist):
        # Increasing ---> Current Hist > Prev Hist
        if s.isEmpty() or hist[i] >= hist[s.peek()]:
            s.push(i)
        # Decreased! ---> Current Hist < Prev Hist (Hisab lagavi do)
        else:
            # Pop hist till Decreasing stops
            while not s.isEmpty() and hist[s.peek()] > hist[i]:
                L = s.pop()
                height = hist[L]
                if s.isEmpty():
                    area = height * i
                else:
                    area = height * (i - s.peek() - 1)
                max_area = max(max_area, area)
            s.push(i)
        i += 1

    while not s.isEmpty():
        L = s.pop()
        height = hist[L]
        area = height * (i - L - 1)
        max_area = max(max_area, area)

    return max_area
コード例 #2
0
def sort_stack(src):
    result = Stack()

    while not src.isEmpty():
        item = src.pop()
        while not result.isEmpty() and item > result.peek():
            src.push(result.pop())
        result.push(item)
    return result
コード例 #3
0
def main():
    source = Stack()

    vals = [1, 3, 2, 5, 7, 100, 30, 12, 4]

    for val in vals:
        source.push(val)

    result = sort_stack(source)
    for i in xrange(result.size()):
        print result.pop()
コード例 #4
0
def Nearest_Smallest(arr):
    result = [-1] * len(arr)
    s = Stack()
    s.push(-1)

    # Scan through entire Array
    for index in xrange(len(arr)):

        # Nearest value is lesser ? Choose it
        if arr[index] > s.peek():
            result[index] = s.peek()
        else:
            # Go till the prev nearest value that's lesser? Choose it
            while arr[index] > s.peek():
                s.pop()
            result[index] = s.peek()

        # Get ready for next iteration
        s.push(arr[index])

    return result
コード例 #5
0
def LargestRectangleInHistogram_optimized(hist):
    s = Stack()
    max_area = 0
    i = 0

    # Scan through Histogram
    while i < len(hist):

        # Increasing ---> Current Hist > Prev Hist
        if s.isEmpty() or hist[i] >= hist[s.peek()]:
            s.push(i)
            i += 1
        # Decreased! ---> Current Hist < Prev Hist (Hisab lagavi do)
        else:
            # Pop hist till Decreasing stops
            L = s.pop()

            # Calculate area
            if s.isEmpty():
                current_area = hist[L] * i
            else:
                current_area = hist[L] * (i - s.peek() - 1)

            # Hisab for max area
            max_area = max(max_area, current_area)

    # Consider all remaining histogram
    while not s.isEmpty():
        L = s.pop()

        # Calculate area
        if s.isEmpty():
            current_area = hist[L] * i
        else:
            current_area = hist[L] * (i - s.peek() - 1)

        # Hisab for max area
        max_area = max(max_area, current_area)

    return max_area
コード例 #6
0
def main():
    str = "AppleKayaKnothingAvid divA"
    mystack = Stack()
    mystack.push(str[0])
    ele = []

    for i in xrange(1, len(str) - 1):
        if str[i + 1] == mystack.peek():
            ele.append(mystack.pop())
        else:
            mystack.push(str[i])

    total = mystack.size()
    end = len(str) - 1

    print "Palindrom strings:"
    for i in range(total):
        palindrom = ""
        temp = mystack.pop()
        while str[end] != temp:
            palindrom += str[end]
            end -= 1
        print palindrom
        end -= 1
コード例 #7
0
def main():
    str = "what is avid diva when kayak agree"
    mystack = Stack()
    mystack.push(str[0])
    mymap = {}
    PalindromStartIndexList = []

    sizeOfPalindromStr = 0
    for i in xrange(1, len(str) - 1):
        if str[i + 1] == mystack.peek():
            # MATCH, Palindrom starts
            if sizeOfPalindromStr == 0:
                PalindromStartIndexList.append(i)
            mystack.pop()
            sizeOfPalindromStr += 1
        else:
            if len(PalindromStartIndexList) != 0:
                mymap[PalindromStartIndexList.pop()] = (sizeOfPalindromStr - 1)
            sizeOfPalindromStr = 0
            mystack.push(str[i])

    # get max from mymap accordig to size
    IndexForLargestPalindrom = max(mymap, key=mymap.get)
    SizeOfLargestPalindrom = mymap[IndexForLargestPalindrom]

    # Build largest palindrom string using index and size
    LargestPalindromStr = ""
    PartPalindromStr = ""

    for i in xrange(IndexForLargestPalindrom + 1,
                    IndexForLargestPalindrom + 1 + SizeOfLargestPalindrom):
        PartPalindromStr += str[i]

    LargestPalindromStr = PartPalindromStr[::-1] + str[
        IndexForLargestPalindrom] + PartPalindromStr
    print LargestPalindromStr
コード例 #8
0
def pda (f, string):

    s = Stack ()
    animation = Tk ()
    w = Canvas(animation, width=1000, height=700, bg = "white")
    w.pack ()
    
    s.push ("Z")
    state = "q"
    seen1 = False
    i = 0
    
    
    
    while string [i] == "0":   
        if state == "q" and s.top () == "X" and string [i] == "0":
            f.write ("(" + state + ", " + string [i:len (string) - 1:] + ", " + s.showStack () + ")" + "|-")
            board (state, string [i:len (string) - 1:], s.showStack (), w)
            s.push ("X")
        elif state == "q" and s.top () == "Z" and string [i] == "0":
            f.write ("(" + state + ", " + string [i:len (string) - 1:] + ", " + s.showStack () + ")" + "|-")
            board (state, string [i:len (string) - 1:], s.showStack (), w)
            s.push ("X")
        i += 1
    while string [i] == "1":
        seen1 = True
        if s.top () == "Z":
            break
    
        if state == "p" and s.top () == "X" and string [i] == "1":
            f.write ("(" + state + ", " + string [i:len (string) - 1:] + ", " + s.showStack () + ")" + "|-")
            board (state, string [i:len (string) - 1:], s.showStack (), w)
            s.pop ()
        elif state == "q" and s.top () == "X" and string [i] == "1":
            f.write ("(" + state + ", " + string [i:len (string) - 1:] + ", " + s.showStack () + ")" + "|-")
            board (state, string [i:len (string) - 1:], s.showStack (), w)
            state = "p"
            s.pop ()
        i += 1


    if state == "p" and string [i::] == "e" and s.top () == "Z":
        #happy road
        f.write ("(" + state + ", " + string [i::] + ", " + s.showStack () + ")" + "|-")
        board (state, string [i::], s.showStack (), w)
        state = "f"
        f.write ("(" + state + ", " + string [i::] + ", " + s.showStack () + ")")
        board (state, string [i::], s.showStack (), w)
        f.write ("\n\nSTRING BELONGS TO L")
    elif state == "p" and string [i::] == "e" and s.top () == "X":
        f.write ("(" + state + ", " + string [i::] + ", " + s.showStack () + ")" + "|-")
        state = "f"
        f.write ("(" + state + ", " + string [i::] + ", " + s.showStack () + ")")
        f.write ("\n\nSTRING DOESN'T BELONG TO L")
    else:
        #caso de solo 1s o 01 y luego 0
        if seen1 == True:
            f.write ("(" + state + ", " + string [i:len (string) - 1:] + ", " + s.showStack () + ")" )
            board (state, string [i:len (string) - 1:], s.showStack (), w)
            if state == "p" and string [i] == "0" and s.showStack () == "Z":
                state = "f"
                f.write ("|-")
                f.write ("(" + state + ", " + string [i:len (string) - 1:] + ", " + s.showStack () + ")")
                board (state, string [i:len (string) - 1:], s.showStack (), w)
            elif state == "p" and string [i] == "1" and s.showStack () == "Z":
                state = "f"
                f.write ("|-")
                f.write ("(" + state + ", " + string [i:len (string) - 1:] + ", " + s.showStack () + ")")
                board (state, string [i:len (string) - 1:], s.showStack (), w)
            elif state == "p" and string [i] == "e" and s.top () == "X":
                f.write ("|-")
                f.write ("(" + state + ", " + string [i::] + ", " + s.showStack () + ")")
                board (state, string [i::], s.showStack (), w)
            
        #caso de solo 0s
        elif seen1 == False:
            f.write ("|-")
            f.write ("(" + state + ", " + string [i::] + ", " + s.showStack () + ")")
            board (state, string [i::], s.showStack (), w)
        f.write ("\n\nSTRING DOESN'T BELONG TO L")

    time.sleep (1)
    animation.destroy ()