Exemplo n.º 1
0
def par_checker(in_str: str):
    """
    >>> par_checker('()')
    True
    >>> par_checker(')(')
    False
    >>> par_checker('()()')
    True
    >>> par_checker('({})')
    True
    """
    s = Stack()
    balanced = True
    index = 0
    while index < len(in_str) and balanced:
        symbol = in_str[index]
        if symbol in OPENS:
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if OPENS.index(top) != CLOSERS.index(symbol):
                    balanced = False
        index += 1
    return balanced and s.isEmpty()
Exemplo n.º 2
0
def test_clear():
    myStack = Stack()
    myStack.push(3)
    myStack.push(4)
    myStack.clear()
    assert myStack.size() == 0
    assert myStack.isEmpty()
    assert myStack.toList() == []
Exemplo n.º 3
0
def balanceCheck(symbols):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbols) and balanced:
        symbol = symbols[index]
        if symbol in "({[":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if not matched(top, symbol):
                    balanced = False
        index += 1
    if balanced and s.isEmpty():
        return True
    else:
        return False
Exemplo n.º 4
0
def revert(s: str):
    stack = Stack()

    for ch in s:
        stack.push(ch)

    res = []
    while not stack.isEmpty():
        res.append(stack.pop())

    return ''.join(res)
Exemplo n.º 5
0
def dectoBin(decimal):
    s = Stack()
    while decimal > 0:
        rem = decimal % 2
        s.push(rem)
        decimal //= 2

    binString = ''
    while not s.isEmpty():
        binString += str(s.pop())

    return binString
Exemplo n.º 6
0
def decToBase(decimal, base):
    alphanumeric = "0123456789ABCDEF"
    remainders = Stack()
    baseString = ''
    while decimal > 0:
        remainder = decimal % base
        remainders.push(remainder)
        decimal //= base

    while not remainders.isEmpty():
        baseString += alphanumeric[remainders.pop()]

    return baseString
Exemplo n.º 7
0
def test_isEmpty_non_empty_stack():
    myStack = Stack()
    myStack.push(1)
    assert not myStack.isEmpty()
Exemplo n.º 8
0
def test_isEmpty_empty_stack():
    myStack = Stack()
    assert myStack.isEmpty()