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()
def test_clear(): myStack = Stack() myStack.push(3) myStack.push(4) myStack.clear() assert myStack.size() == 0 assert myStack.isEmpty() assert myStack.toList() == []
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
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)
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
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
def test_isEmpty_non_empty_stack(): myStack = Stack() myStack.push(1) assert not myStack.isEmpty()
def test_isEmpty_empty_stack(): myStack = Stack() assert myStack.isEmpty()