コード例 #1
0
ファイル: par_checker.py プロジェクト: freeman1981/ads
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()
コード例 #2
0
ファイル: test_stack.py プロジェクト: maxotar/datastructures
def test_clear():
    myStack = Stack()
    myStack.push(3)
    myStack.push(4)
    myStack.clear()
    assert myStack.size() == 0
    assert myStack.isEmpty()
    assert myStack.toList() == []
コード例 #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
コード例 #4
0
ファイル: reverse_string.py プロジェクト: freeman1981/ads
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)
コード例 #5
0
ファイル: dectobin.py プロジェクト: knopthakorn/Python
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
コード例 #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
コード例 #7
0
ファイル: test_stack.py プロジェクト: maxotar/datastructures
def test_isEmpty_non_empty_stack():
    myStack = Stack()
    myStack.push(1)
    assert not myStack.isEmpty()
コード例 #8
0
ファイル: test_stack.py プロジェクト: maxotar/datastructures
def test_isEmpty_empty_stack():
    myStack = Stack()
    assert myStack.isEmpty()