def lvp(s):
    stk = Stack()
    max_len = 0
    curr_len = 0

    for c in s:
        if c == '(':
            stk.push(c)
        else: # c == ')'
            if stk.pop() is None:
                max_len = curr_len if curr_len > max_len else max_len
                curr_len = 0
            else:
                curr_len += 2

    return curr_len if curr_len > max_len else max_len
Exemple #2
0
def parChecker(symbolString):
    '''
    General Balanced Symbol Problem
    '''
    s = Stack()
    index = 0
    balanced = True

    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
Exemple #3
0
def postfixEval(postfixExpr):
    operandStack = Stack()
    tokenList = postfixExpr.split()

    for token in tokenList:
        if token in "0123456789":
            operandStack.push(int(token))
        else:
            operand2 = operandStack.pop()
            operand1 = operandStack.pop()
            result = doMath(token, operand1, operand2)
            operandStack.push(result)
    return operandStack.pop()
Exemple #4
0
def lvp(s):
    if s is None:
        return 0

    stk = Stack()
    max_len = 0
    curr_len = 0

    for c in s:
        print "Evaluating char:", c
        if c == "(":
            stk.push(c)
            continue

        try:
            stk.pop()
            curr_len += 2
            print "TRY BLOCK: curr_len =", curr_len
        except Exception as e:
            max_len = max(max_len, curr_len)
            curr_len = 0
            print "Max Len =", max_len, "Curr Len =", curr_len

    print "Finally Max Len =", max_len, "Curr Len =", curr_len
    return max(max_len, curr_len)
def removeLongestSubstring(s):
    stk = Stack()
    #stk.push(-1)
    maxLen = 0
    currLen = 0

    for i in range(len(s)-1, -1, -1):
        char = s[i]
        if char == '0':
            stk.push(char)
            if currLen > 0:
                currLen = 0
            continue

        # char == '1'
        # pop 2 0's for every 1.
        j = 0
        while not stk.isEmpty and j < 2:
            stk.pop()
            j += 1

        if j == 2:
            currLen += 3
        
        maxLen = max(currLen, maxLen)

    return maxLen
 def reverseString(self, originalString):
     stk = Stack()
     
     for i in originalString:
         stk.push(i)
     
     print(stk.items)
     
     while not stk.isEmpty():
         print(stk.pop())
Exemple #7
0
def baseConverter(decNumber, base):
    '''
    Method to convert decimal to any base
    '''
    digits = "0123456789ABCDEF"

    remStack = Stack()

    while decNumber > 0:
        rem = decNumber % base
        remStack.push(rem)
        decNumber = decNumber // base

    baseString = ""
    while not remStack.isEmpty():
        baseString = baseString + digits[remStack.pop()]
    return baseString
Exemple #8
0
def infixToPrefix(infixexpr):
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1

    infixexpr = infixexpr[::-1]

    opStack = Stack()
    prefixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
            prefixList.append(token)
        elif token == "(":
            opStack.push(token)
        elif token == ")":
            topToken = opStack.pop()
            while topToken != "(":
                prefixList.append(token)
                topToken = opStack.pop()
Exemple #9
0
def infixToPostfix(infixexpr):
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1

    opstack = Stack()
    postfixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYYZ" or token in "123457890":
            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)
Exemple #10
0
def reverse_stack(stack):
    r_stk = Stack()
    while not stack.isEmpty():
        r_stk.push(stack.pop())

    return r_stk
Exemple #11
0
#!/usr/bin/python

from ds.Stack import Stack


def reverse_stack(stack):
    r_stk = Stack()
    while not stack.isEmpty():
        r_stk.push(stack.pop())

    return r_stk


if __name__ == "__main__":
    stk = Stack()
    stk.push(4)
    stk.push(3)
    stk.push(2)
    stk.push(1)

    print stk.items
    rev_stk = reverse_stack(stk)
    print rev_stk.items