def cleanstack(ip):
    length = 0
    cleaned = ArrayStack()

    while not ip.isempty:
        if cleaned.top != ip.top:
            cleaned.push(ip.top)
            ip.pop()
            length += 1
        else:
            ip.pop()

    return cleaned, length
def test_isduplindrome_two():
    ip = ArrayStack()
    ip.push('m')
    ip.push('a')
    ip.push('d')
    ip.push('a')
    ip.push('m')

    assert (isduplindrome(ip) == True)
def isstackpalindrome(ip, length, temp=None):
    print ip.stack

    if not temp:
        temp = ArrayStack()

    odd = False
    if length % 2 != 0:
        odd = True

    for _ in range(0, int(length / 2)):
        temp.push(ip.top)
        ip.pop()

    if odd:
        ip.pop()

    while not ip.isempty:
        if ip.top != temp.top:
            return False

        ip.pop()
        temp.pop()

    return True
def test_isduplindrome_three():
    ip = ArrayStack()
    ip.push('m')
    ip.push('a')
    ip.push('d')
    ip.push('m')

    assert (isduplindrome(ip) == False)
Пример #5
0
def postfix_evaluation(items):
    """Evaluate Reverse Polish Notation."""
    stack = ArrayStack()

    op = ["+", "-", "*", "/"]

    for item in items:
        if item not in op:
            stack.push(int(item))
        else:
            op2 = stack.top
            stack.pop()

            op1 = stack.top
            stack.pop()

            if item == "+":
                ans = int(op1 + op2)
                stack.push(ans)
            elif item == "-":
                ans = int(op1 - op2)
                stack.push(ans)
            elif item == "*":
                ans = int(op1 * op2)
                stack.push(ans)
            elif item == "/":
                ans = int(float(op1) / float(op2))
                stack.push(ans)
            else:
                raise IOError("Invalid operator specified - {0}".format(item))

    return stack.top
Пример #6
0
def prefix_evaluation(items):
    """Evaluate Prefix Notation."""
    stack = ArrayStack()

    op = ["+", "-", "*", "/"]

    for item in reversed(items):
        if item not in op:
            stack.push(int(item))
        else:
            op1 = stack.top
            stack.pop()

            op2 = stack.top
            stack.pop()

            if item == "+":
                ans = int(op1 + op2)
                print "{0} + {1} = {2}".format(op1, op2, ans)
                stack.push(ans)
            elif item == "-":
                ans = int(op1 - op2)
                stack.push(ans)
            elif item == "*":
                ans = int(op1 * op2)
                stack.push(ans)
            elif item == "/":
                ans = int(float(op1) / float(op2))
                stack.push(ans)
            else:
                raise IOError("Invalid operator specified - {0}".format(item))

    return stack.top