def postfix_eval(postfix_expr):
    operand_stack = Stack()
    tokenList = postfix_expr.split()

    for token in tokenList:
        if token in '0123456789':
            operand_stack.push(int(token))
        else:
            operand2 = operand_stack.pop(
            )  # The last number popped is the second operand
            operand1 = operand_stack.pop()
            result = do_math(token, operand1, operand2)
            operand_stack.push(result)
    return operand_stack.pop()
def divide_by_two(number):
    remainder_stack = Stack()  # Create an empty stack

    while number > 0:
        remainder = number % 2
        remainder_stack.push(remainder)
        number = number // 2
# remainder_stack.display()  Prints the stack in list form
    result = ''
    while not remainder_stack.is_empty():
        result = result + str(remainder_stack.pop())
    return result
def base_conversion(number, base):
    digits = '0123456789ABCDEF'
    remainder_stack = Stack()

    while number > 0:
        remainder = number % base
        remainder_stack.push(remainder)
        number = number // base

    result = ''
    while not remainder_stack.is_empty():
        result = result + str(digits[remainder_stack.pop()])

    return result
def par_checker(symbol_strng):
    s = Stack()  # Empty stack to place parentheses
    balanced = True
    index = 0

    while index < len(symbol_strng) and balanced:
        symbol = symbol_strng[index]  # First parentheses is at index 0
        if symbol in "([{":
            s.push(symbol)
        else:
            if s.is_empty():
                balanced = False
            else:
                top_open = s.pop()
                if not matches(top_open, symbol):
                    balanced = False
        index += 1

    return balanced and s.is_empty()
Example #5
0
def is_valid(expression):
    # Create an empty stack
    par_stack = Stack()

    for ch in expression:
        if ch in '({[':
            par_stack.push(ch)
        if ch in ')}]':
            if par_stack.is_empty():
                print("Right parentheses are more then left parentheses.")
                return False
            else:
                popped_char = par_stack.pop()
                if not matched_parentheses(popped_char, ch):
                    print("Mismatched parentheses are ", popped_char, " and ", ch)
                    return False

    if par_stack.is_empty():
        print("Balanced parentheses")
        return True
    else:
        print("Left parentheses are more than right parentheses")
        return False
def infix_to_postfix(infix_expr):
    # Prec holds the precedence
    prec = {}
    prec['**'] = 4
    prec['*'] = 3
    prec['/'] = 3
    prec['+'] = 2
    prec['-'] = 2
    prec['('] = 1

    operator_stack = Stack()  # Empty stack for the operators
    postfixList = []  # Empty stack for the post fix list
    tokenList = infix_expr.split()  # Lets split the expression

    # Lets read each token
    for token in tokenList:
        if token in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or token in '0123456789':
            # Append token to the postfixList
            postfixList.append(token)
        elif token == '(':
            # Push the token to operator stack
            operator_stack.push(token)
        elif token == ')':
            # pop last item from operator stack
            topToken = operator_stack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = operator_stack.pop()
        else:
            while (not operator_stack.is_empty()) and (
                    prec[operator_stack.peek()] >= prec[token]):
                postfixList.append(operator_stack.pop())
            operator_stack.push(token)

    while not operator_stack.is_empty():
        postfixList.append(operator_stack.pop())
    return " ".join(postfixList)
Example #7
0
from Stack1 import Stack

s = Stack()

print s.isEmpty()
s.push("s")

print s.size()
s.push("apple")
print s.isEmpty()

s.push("big")
print s.pop()
print s.pop()
print s.size()
print s.pop()
s.pop()
def postfix_evaluation(postfix_expr):
    # Create an empty stack
    st = Stack()

    # Traverse the expression
    for symbol in postfix_expr:
        if symbol.isdigit():
            st.push(int(symbol))
        else:
            x = st.pop()
            y = st.pop()

            if symbol == '+':
                st.push(y + x)
            elif symbol == '-':
                st.push(y - x)
            elif symbol == '*':
                st.push(y * x)
            elif symbol == '/':
                st.push(y / x)
            elif symbol == '%':
                st.push(y % x)
            elif symbol == '^':
                st.push(y**x)
    return st.pop()
from Stack1 import Stack
num_string_stack = Stack()  # Create an empty stack


def to_string(n, base):
    num_to_String = '0123456789ABCDEF'

    while n > 0:
        if n < base:
            num_string_stack.push(num_to_String[n])
        else:
            num_string_stack.push(num_to_String[n % base])
        n = n // base

    result = ''
    while not num_string_stack.is_empty():
        result = result + str(num_string_stack.pop())
    return result


if __name__ == '__main__':
    print(to_string(10, 2))
Example #10
0
def infix_2_postfix(infix_expr):
    postfix = ""  # empty string where operands operators and popped items from stack will go
    operator_stack = Stack()  # Empty stack to push items

    for symbol in infix_expr:
        if symbol == " " or symbol == '\t':  # Ignore white spaces
            continue

        if symbol == '(':
            operator_stack.push(symbol)
        elif symbol == ')':
            top_symbol_stack = operator_stack.pop()
            while top_symbol_stack != '(':
                postfix += top_symbol_stack
                top_symbol_stack = operator_stack.pop()
        elif symbol in '+-*/%^':
            while not operator_stack.is_empty() and precedence(
                    operator_stack.peek()) >= precedence(symbol):
                # If the precedence in the stack is >= scanned symbol pop from stack and add to postfix list
                postfix += operator_stack.pop()
            operator_stack.push(
                symbol)  # When finished push the scanned symbol
        else:  # Its an operand add it to postfix string
            postfix += symbol

    while not operator_stack.is_empty():
        postfix += operator_stack.pop()
    return postfix