Exemplo n.º 1
0
def check_parentheses(symbol_string):
    s = Stack()
    balanced = True
    index = 0
    matcher = {
        '}': '{',
        ']': '[',
        ')': '(',
    }
    while index < len(symbol_string) and balanced:
        symbol = symbol_string[index]
        if symbol in ['(', '[', '{']:
            s.push(symbol)
        elif symbol in [')', ']', '}']:
            if s.is_empty() or matcher[symbol] != s.peek():
                balanced = False
            else:
                s.pop()

        index = index + 1

    if balanced and s.is_empty():
        return True
    else:
        return False
Exemplo n.º 2
0
def convert_infix_to_prefix(infix_expression):
    reverse = reverse_string(infix_expression)
    operators = Stack()
    prefix_expr = []
    ops = '(+-*/**)'
    expr_list = list(reverse)

    for token in expr_list:
        if token not in ops:
            prefix_expr.append(token)
        elif token == ')':
            operators.push(token)
        elif token == '(':
            while operators.peek() != ')':
                prefix_expr.append(operators.pop())
            operators.pop()
        else:
            while (not operators.is_empty()
                   ) and precedence[operators.peek()] > precedence[token]:
                prefix_expr.append(operators.pop())
            operators.push(token)

    while not operators.is_empty():
        prefix_expr.append(operators.pop())

    return reverse_string(''.join(prefix_expr))
Exemplo n.º 3
0
def test_push():
    my_stack = Stack()
    my_stack.push(0)
    my_stack.push(1)
    assert my_stack.items == [0, 1]
    assert my_stack.size() == 2
    assert my_stack.is_empty() is False
Exemplo n.º 4
0
def check_parentheses_bk(symbol_string):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbol_string) and balanced:
        symbol = symbol_string[index]
        if symbol in '{[(':
            s.push(symbol)
        elif symbol in ')]}':
            if s.is_empty() or not matches(s.pop(), symbol):
                balanced = False

        index = index + 1

    if balanced and s.is_empty():
        return True
    else:
        return False
Exemplo n.º 5
0
def convert_decimal_to_binary(decimal_number):
    s = Stack()
    while decimal_number > 0:
        s.push(decimal_number % 2)
        decimal_number = decimal_number // 2

    num = ""
    while not s.is_empty():
        num = num + str(s.pop())

    return num
Exemplo n.º 6
0
def convert_decimal_to_base(decimal_number, base):
    digits = "0123456789ABCDE"
    s = Stack()
    while decimal_number > 0:
        s.push(decimal_number % base)
        decimal_number = decimal_number // base

    num = ""
    while not s.is_empty():
        num = num + digits[s.pop()]

    return num
Exemplo n.º 7
0
def convert_infix_to_postfix_new(infix_expr):
    op = Stack()
    postfix = []
    infix = list(infix_expr)
    for index in range(len(infix)):
        token = infix[index]
        if token not in ops:
            postfix.append(token)
        elif token == '(':
            op.push(token)
        elif token == ')':
            while op.peek() != '(':
                postfix.append(op.pop())
            op.pop()
        else:
            while (not op.is_empty()) and precedence[op.peek()] >= precedence[infix[index]]:
                postfix.append(op.pop())
            op.push(token)

    while not op.is_empty():
        postfix.append(op.pop())

    print(''.join(postfix))
Exemplo n.º 8
0
def dec_to_binary(x):
    # Return binary representation of base 10 (decimal) number.

    exponents = Stack()

    while x > 0:
        remainder = x % 2
        exponents.push(remainder)
        x = x // 2

    binary = ''

    while not exponents.is_empty():
        binary += str(exponents.pop())

    return binary
Exemplo n.º 9
0
def convert_infix_to_postfix(infix_expr):
    op = Stack()
    postfix = []
    infix = list(infix_expr)
    for index in range(len(infix)):
        if infix[index] in ops:
            while (not op.is_empty()) and precedence[op.peek()] >= precedence[infix[index]]:
                if op.peek() == '(':
                    op.pop()
                else:
                    postfix.append(op.pop())
            if infix[index] != ')':
                op.push(infix[index])
        elif infix[index] != ' ':
            postfix.append(infix[index])

    while op.size() > 0:
        postfix.append(op.pop())

    print(''.join(postfix))
Exemplo n.º 10
0
def validate_html_tags(html_code: str):
    tags = Stack()
    balanced = True
    start_index = int(html_code.find('<')) + 1
    end_index = int(html_code.find('>'))

    while balanced and start_index > 0 and end_index > 0:
        tag = html_code[start_index:end_index]
        # print("start -> {} , End -> {} ; Tag -> {}".format(str(start_index), str(end_index), tag))
        if not tag.startswith('/'):
            tags.push(tag)
        else:
            if tags.is_empty():
                balanced = False
                break
            peek_tag = '/' + tags.pop()
            if peek_tag != tag:
                balanced = False

        start_index = int(html_code.find('<', end_index)) + 1
        end_index = int(html_code.find('>', start_index))

    return balanced
Exemplo n.º 11
0
def test_initialization():
    my_stack = Stack()
    assert my_stack.items == []
    assert my_stack.size() == 0
    assert my_stack.is_empty()